本文介绍了替代 <input type='file'/> 中的 onchange 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看似很简单的事情,却想不通.我一直在 <input type=file/> 元素上使用 onchange 事件,它运行良好.一旦用户浏览并选择了一个文件,我就会获取路径并使用我的自定义 js 函数上传它.

Seems really a simple thing but can't figure it out. I've been using the onchange event on <input type=file /> element and it works well. Once the user browses and selects a file, I get the path and upload it using a my custom js function.

问题是如果用户连续两次选择同一个文件,这不起作用,onchange 不会触发(这是有道理的,因为没有任何改变)但在我的情况下这很重要让我也捕获该事件,获取路径并上传.

The problem is this doesn't work if a user selects the same file twice in a row, the onchange doesn't fire (which makes sense since nothing changed) but in my case it's important for me to capture that event too, get the path and upload.

(类似于使用jQuery清除,不确定我是否应该将其解析为重复项)

(Similar to Clearing <input type='file' /> using jQuery, not sure if I should resolve this as duplicate)

推荐答案

您可以删除输入并使用 javascript 创建一个相同的输入 - 新输入将是空的.

You can just remove the input and create an identical one with javascript - the new one will be empty.

(编辑答案直截了当,评论现在无关紧要)

(edited answer to be straight to the point, comments are irrelevant now)

这篇关于替代 &lt;input type='file'/&gt; 中的 onchange 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 06:18