HTML输入文件选择事件在选择相同文件时不会触发

HTML输入文件选择事件在选择相同文件时不会触发

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

问题描述

是否有机会检测用户为

将输入的值设置为 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输入文件选择事件在选择相同文件时不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:51