HTML输入文件选择事件在选择相同文件时不会触发 [英] HTML input file selection event not firing upon selecting the same file
问题描述
是否有机会检测用户为文件
元素的HTML input
所做的每个文件选择?
之前曾询问过很多次,但通常建议的 onchange
事件不会触发,如果用户选择同样的文件。
将输入
的值设置为 null
在每个 onclick
事件上。这将重置输入
的值,即使选择了相同的路径,也会触发 onchange
事件。
input.onclick = function(){
this.value = null;
};
input.onchange = function(){
alert(this.value);
};
注意:如果您的文件前缀为'C:\' fakepath\。这是一种防止JavaScript知道文件绝对路径的安全功能。浏览器仍然在内部知道它。
Is there any chance to detect every file selection the user made for an HTML input
of type file
element?
This was asked many times before, but the usually proposed onchange
event doesn't fire if the user select the same file again.
Set the value of the input
to null
on each onclick
event. This will reset the input
's value and trigger the onchange
event even if the same path is selected.
input.onclick = function () {
this.value = null;
};
input.onchange = function () {
alert(this.value);
};
Here's a DEMO.
Note: It's normal if your file is prefixed with 'C:\fakepath\'. That's a security feature preventing JavaScript from knowing the file's absolute path. The browser still knows it internally.
这篇关于HTML输入文件选择事件在选择相同文件时不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!