选择相同文件时,HTML输入文件选择事件不会触发(HTML input file selection event not firing upon selecting the same file)

编程入门 行业动态 更新时间:2024-10-09 03:19:45
选择相同文件时,HTML输入文件选择事件不会触发(HTML input file selection event not firing upon selecting the same file)

有没有机会检测用户为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.

更多推荐

本文发布于:2023-08-04 01:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   事件   input   file   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!