处理同一页面中借助form+input[type="file"]上传图片出现的input无法清空问题

简介:        今天下午帮同事改了这样一个bug:                       在一个页面中对多张图进行上传时,由于input的value无法情况的问题,导致每次选完图片后,都跟第一张图片一样,无法出现如下效果:                 ...



       今天下午帮同事改了这样一个bug:

          

           在一个页面中对多张图进行上传时,由于input的value无法情况的问题,导致每次选完图片后,都跟第一张图片一样,无法出现如下效果:


        



          

                百度了下思路:先将input取到,然后放到一个临时form里面清空,最后删掉form.



          代码如下:

  

          首先是生成图片选择和显示部分的绑定代码:


        

 var img_tmp = '<tr><td><input type="text" name="code" value="" style="width: 90%; display: none;"><input type="text" name="title[]" style="width:90%;" /></td><td><input type="text" name="sort[]" style="width:90%;" /></td><td><input type="text" name="num[]" style="width:90%;"/></td>' +
                                                '<td><img alt="" src="" id="" name="img[]" style="max-width: 100px; max-height: 100px; display: block; float: left; border: 1px solid #E6E7EB;" /><div class="file_box">' +
                                                '<input type="button" class="btn" value="选择图片" />'+
                                                '<input type="file" name="fileLOGO" class="file" id="" style="width: 80%;" onchange="UploadImages(this)" />' +
                                                '</div></td>' +
                                                '<td><a class="del" href="javascript:void(0)">删除</a></td></tr>';


           在onchange="UploadImages(this)" 这里的方法中,我们传入本次点击的input,进行提交:



       

//多图片上传       
        function UploadImages(FileInput) {
            //var isno = fileChange($(FileInput));
            //if (isno == undefined) {
            var options = {
                type: "POST",
                url: '../Handler/AshxUploadFile.ashx?type=Images',
                success: function (msg) {
                    if (msg == "error") {
                        clearImages($(FileInput));
                        showerrortip("上传失败");
                    } else {
                        if (msg != "errortype") {
                            clearImages($(FileInput));
                            $(FileInput).parent().prev("img").attr("src", strPic + msg);
                            clearImages($(FileInput));
                            $(FileInput).attr("style", "");

                        }
                    }

                }, error: function (msg) {
                    clearImages($(FileInput));
                    $(FileInput).attr("style", "");
                    showerrortip("参数异常!");
                }
            };
                // 将options传给ajaxForm
                $('form').ajaxSubmit(options);
               

            //} 

        }



          当时因为忽略了对input的清理,才导致了那个问题,为此,加入clearImages函数:



              

function clearImages(selector) {
            var fi;
            var sourceParent;

            if (selector) {
                fi = $(selector);
                sourceParent = fi.parent();
            }
            else {
                return;
            }

            $("<form id='tempForm'></form>").appendTo(document.body);

            var tempForm = $("#tempForm");

            tempForm.append(fi);
            tempForm.get(0).reset();

            sourceParent.append(fi);
            tempForm.remove();


        }

          这样,就利用临时form清空了input.









目录
相关文章
|
2月前
|
移动开发 自然语言处理 前端开发
input表单 type属性详解
input 元素可以用来生成一个供用户输入数据的简单文本框。 在默认的情况下, 什么样的数据均可以输入。而通过不同的type属性值,可以限制输入的内容。
87 1
|
11月前
Input 标签监听内容输入(change、input 事件区别)
Input 标签监听内容输入(change、input 事件区别)
87 0
|
2月前
使用Form报错提示If ngModel is used within a form tag, either the name attribute must be set or the form
使用Form报错提示If ngModel is used within a form tag, either the name attribute must be set or the form
|
9月前
|
数据安全/隐私保护
input表单的23个type属性
input表单的23个type属性
|
10月前
input type=file过滤图片
input type=file过滤图片
41 0
HTML中<button />和<input type=“button“/>的区别
HTML中<button />和<input type=“button“/>的区别
67 0
|
算法 JavaScript
你不知道的<input type="file">的小秘密
你不知道的<input type="file">的小秘密
你不知道的<input type="file">的小秘密
ADI
|
JavaScript 前端开发 API
[记录] input[type=file]属性详解
[记录] input[type=file]属性详解
ADI
262 0
|
前端开发
使input type=“text“不可编辑
使input type=“text“不可编辑
297 0
使input type=“text“不可编辑