如何检测输入类型=文件“更改"对于同一个文件? [英] How to detect input type=file "change" for the same file?

查看:18
本文介绍了如何检测输入类型=文件“更改"对于同一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户选择文件时触发一个事件.使用 .change 事件这样做,如果用户每次都更改文件,它就会起作用.

I want to fire an event when the user select a file. Doing so with .change event it works if the user changes the file every time.

但是如果用户再次选择相同的文件,我想触发该事件.

But I want to fire the event if the user select the same file again.

  1. 用户选择文件A.jpg(事件触发)
  2. 用户选择文件B.jpg(事件触发)
  3. 用户选择文件 B.jpg(事件不触发,我希望它触发)
  1. User select file A.jpg (event fires)
  2. User select file B.jpg (event fires)
  3. User select file B.jpg (event doesn't fire, I want it to fire)

我该怎么做?

推荐答案

你可以欺骗它.删除文件元素并将其添加到 change 事件的相同位置.它将删除文件路径,使其每次都可以更改.

You can trick it. Remove the file element and add it in the same place on change event. It will erase the file path making it changeable every time.

jsFiddle 示例.

或者你可以简单地使用 .prop("value", ""),参见 jsFiddle 上的这个例子.

Or you can simply use .prop("value", ""), see this example on jsFiddle.

  • jQuery 1.6+ prop
  • 早期版本attr

这篇关于如何检测输入类型=文件“更改"对于同一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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