当前位置:Gxlcms > html代码 > 处理同一页面中借助form+input[type="file"]上传图片出现的input无法清空问题_html/css_WEB-ITnose

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

时间:2021-07-01 10:21:17 帮助过:19人阅读

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


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





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



代码如下:


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


 var img_tmp = '' +                                                '
' + ''+ '' + '
' + '删除';


在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;            }            $("").appendTo(document.body);            var tempForm = $("#tempForm");            tempForm.append(fi);            tempForm.get(0).reset();            sourceParent.append(fi);            tempForm.remove();        }

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

人气教程排行