简体   繁体   English

为什么选择的多个文件在chrome浏览器上不起作用?

[英]Why multiple files selected doesn't work on chrome browser?

Following js code is for html5 multiple files selected [duplicate] that doesn't work for chrome browser, after selecting a file homonymous several times. 以下js代码是针对html5选定的多个文件,这些文件在重复选择了多个同名文件后选择[重复],这些文件不适用于chrome浏览器。

For EX: selecting file admin.png for 2 or up times tandem. 对于EX:连续2倍或以上的时间选择文件admin.png It only alert for first times. 它仅在第一次警报。

DEMO (This doesn't work only in chrome browser ): http://jsfiddle.net/s9mt4/ 演示(仅在chrome浏览器中不起作用): http : //jsfiddle.net/s9mt4/

function doClick() {
    var el = document.getElementById("fileElem");
    if (el) {
        el.click();
    }
}
function handleFiles(files) {
    var d = document.getElementById("fileList");

var elementArray = document.getElementsByClassName("ImgNameUp");
var ReValue = true;
for (var i = 0; i < elementArray.length; ++i){
    if(elementArray[i].innerHTML == files[0].name){
        ReValue = false;
    }
}
$('.ImgNameUp2').append('<div class="ImgNameUp">'+files[0].name+'</div>')
    if (ReValue) {
        alert('true');
    } else {
        alert('false');
    }
}

what do i do,change in code that it working right? 我该怎么做,更改可以正常工作的代码?

Your input fields are listening to the onchange event to fire the javascript. 您的输入字段正在监听onchange事件以触发javascript。

According to W3C's document : 根据W3C的文档

onchange event occurs when a control loses the input focus and its value has been modified since gaining focus 当控件失去输入焦点并且自获得焦点以来其值已被修改时,会发生onchange事件

if you try to upload the same file, the value of file input does not change so does not fire the function. 如果您尝试上传相同的文件,则文件输入的值不会更改,因此不会触发该功能。 I think Chrome is the only browser to implement this "correctly". 我认为Chrome浏览器是唯一可以“正确”实现此功能的浏览器。

If you want to upload twice, clear file input value: 如果要上传两次,请清除文件输入值:

function doClick() {
    var el = document.getElementById("fileElem");
    $(el).val(null); // <-- this line
    if (el) {
        el.click();
    }
}

http://jsfiddle.net/s9mt4/2/ http://jsfiddle.net/s9mt4/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM