javascript - jquery 如何检测文件输入元素的更改?

标签 javascript jquery html file

我正在将图像上传功能构建到 Web 应用程序中,并使用以下事件处理程序:

$('input:file').change(function(){
    //handle input change
});

我发现,如果我尝试两次上传相同的图像,则不会调用处理程序,这是有道理的,因为从技术上讲,输入没有改变。但我想知道,需要发生什么才能触发更改?以下是我正在寻找的一些示例:

  • 更改文件名
  • 更改文件内容,无论文件名如何
  • 更改文件类型

其中哪一个会导致文件作为更改的文件被接收?或者还有其他场景吗?

最佳答案

<强> Refer this Link

输入类型 - 文件:

The onchange event is fired when the contents have changed. If the user types the path (does not use the Browse button) in Internet Explorer, then the onchange event is fired only when the element loses the focus. Therefore use the onpropertychange event to detect the modification in Internet Explorer.

<强> MSDN reference

Fires when the contents of the object or selection have changed.

我的测试结果:

  1. 更改文件名:更改触发。
  2. 更改内容:保持文件名相同,更改事件不会触发。
  3. 更改文件类型:保持文件名相同,“更改”将触发。
  4. IE 9,10中,如果您尝试更改文件名(在输入字段中),则会触发更改事件并清除文件名字段。

所以,这个故事的寓意是,内容更改意味着文件名更改。

关于javascript - jquery 如何检测文件输入元素的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24245670/

相关文章:

javascript - 为什么第二个for循环需要+i?

javascript - 当没有其他 div 滚动时滚动 div

javascript - 输出包含 3 个单词的字符串中的前 2 个单词

javascript - 通过 service-worker 的请求完成了两次

javascript - 如何将自定义样式规则保存到LocalStorage?

java - 如何使用 jquery 获取带有换行符的 XML?

html - 没有显式设置宽度的 float 元素

javascript - 如何使用 CSS3 和 HTML5 使部分 div 渐变透明?

html - 使用CSS拉伸(stretch)内容以适合桌面页面

javascript - 我不想两次调用自定义函数