HTML输入文件选择事件在选择相同文件时不会触发 [英] HTML input file selection event not firing upon selecting the same file

查看:146
本文介绍了HTML输入文件选择事件在选择相同文件时不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有机会检测用户为文件元素的HTML input 所做的每个文件选择?

之前曾询问过很多次,但通常建议的 onchange 事件不会触发,如果用户选择同样的文件。

解决方案

输入的值设置为 null 在每个 onclick 事件上。这将重置输入的值,即使选择了相同的路径,也会触发 onchange 事件。

  input.onclick = function(){
this.value = null;
};

input.onchange = function(){
alert(this.value);
};

以下是 DEMO



注意:如果您的文件前缀为'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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆