输入文件onchange事件未触发 [英] Input File onchange event not firing

查看:101
本文介绍了输入文件onchange事件未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次尝试选择文件时,我都尝试触发onchange事件.如果我选择的文件不同,它会触发,但是,即使两次选择相同的文件,我也要触发.

I am trying to fire onchange event everytime a file is selected. It does fire if the file I select is different, however, I wanted to fire even if the same file is selected twice.

html

  <input name="file" type="file"  (change)="onChange($event)" style="width:80%" />

组件

onChange(event: any) {
   let files = event.srcElement.files;
   this.files = files;
   event= null;
}

推荐答案

实现此跨浏览器且无需更改大量代码的最可靠方法是,将输入的值设置为单击时的null.

The most reliable way to achieve this cross browser and without having to change much code is to set the value of the input to null on click.

onclick="this.value = null"

所以您的输入看起来像这样

So your input would look like this

<input name="file" type="file" onclick="this.value = null" (change)="onChange($event)" style="width:80%"/>

这是一个有效的示例: plnkr

Here is an working example: plnkr

这篇关于输入文件onchange事件未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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