【问题标题】:onchange event fires after clearing value清除值后触发 onchange 事件
【发布时间】:2018-07-04 05:38:44
【问题描述】:

我有一个文件上传控件,文件上传清除后如下。

<input type="file" id="flupload1" **onchange="angular.element(this).scope().UploadPoster(event)"** file-upload /><br />

//文件上传后清除

document.getElementById('flupload1').value = "";

问题是再次清除文件上传事件触发后,文件上传功能再次上传。

但这种行为只在 IE 中表现。

清除值后如何停止事件触发?

【问题讨论】:

    标签: javascript jquery html angularjs


    【解决方案1】:

    这个功能可以帮到你:

    function clearFileInput(id) 
    { 
        let oldInput = document.getElementById(id); 
        let newInput = document.createElement("input"); 
        newInput.type = "file"; 
        newInput.id = oldInput.id; 
        newInput.name = oldInput.name; 
        newInput.className = oldInput.className; 
        newInput.style.cssText = oldInput.style.cssText; 
        // TODO: copy any other relevant attributes 
        oldInput.parentNode.replaceChild(oldInput.cloneNode(), oldInput);
    }
    clearFileInput("fileInput");
    

    【讨论】:

    • 设置只读不是一个好主意。由于只读,上传不允许下次上传文件。
    • @ShakeerHussain 如果你想防止用户不上传文件,这是最好的解决方案
    • 我并不是要阻止。我的问题是在再次触发文件上传触发并再次上传文件后清除值。
    • 好的,然后创建一个临时变量并在第一次文件上传时设置它然后在第二次文件上传函数被调用时检查它只需检查临时变量并检查文件是否为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    相关资源
    最近更新 更多