有没有机会检测用户为file元素类型的HTML input所做的每个文件选择?
以前被问过很多次,但如果用户再次选择相同的文件,通常提出的onchange事件不会触发。
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.
最满意答案
在每个onclick事件上将input的值设置为null 。 即使选择了相同的路径,这将重置input的值并触发onchange事件。
input.onclick = function () { this.value = null; }; input.onchange = function () { alert(this.value); };这是一个DEMO 。
注意:如果您的文件以“C:\ fakepath”为前缀,那是正常的。 这是一个安全功能,可以防止JavaScript了解文件的绝对路径。 浏览器内部仍然知道。
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.
更多推荐
发布评论