我正在将图像上传功能构建到 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.
我的测试结果:
- 更改文件名:更改触发。
- 更改内容:保持文件名相同,更改事件不会触发。
- 更改文件类型:保持文件名相同,“更改”将触发。
- 在IE 9,10中,如果您尝试更改文件名(在输入字段中),则会触发更改事件并清除文件名字段。
所以,这个故事的寓意是,内容更改意味着文件名更改。
关于javascript - jquery 如何检测文件输入元素的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24245670/