---
title: "input file控件onchange事件解决方案"
author: "luolonghao"
date: "2011-08-29T14:17:44Z"
tags: []
categories: ["Web前端"]
slug: "input-file-onchange"
draft: false
lastmod: "2011-10-26T14:33:46Z"
id: 1160389
url: "https://luolonghao.iteye.com/blog/1160389"
---

问题：

在IE上选择相同文件时无法触发onchange事件，inputFile.value = ''也没用。


解决方案：

新建一个form，将input file放在form里，form.reset()，最后把form移除。


```js

// 例子是基于jQuery，原生或其它类库思路一样
var tempForm = document.createElement('form');
$(inputFile).before(tempForm);
$(tempForm).append(inputFile);
tempForm.reset();
$(tempForm).after(inputFile);
$(tempForm).remove();

```


还有一个方法是移除input file后重新创建，但这个会丢失已绑定的相关事件，不太好用。