重置 file input

有时用户上传相同附件时也需要触发input[type='file']的change事件,除了将form重置外,还可以将input的value设为空

<input type="file" id="test" name="test" multiple>  

javascript:

var fileinput = document.getElementById('test');
fileinput.onchange = function() {
    if (this.value) {
        console.log(this.value);
        //重置,IE11会触发change事件
        this.value = '';
        //IE9,IE10需要重置type
        this.type = 'text'
        this.type = 'file'
    } else {
        //value设为''触发的change(IE11)
        console.log("已重置")
    }
}

 

已测试的浏览器:Chrome, Firefox, IE11,通过IE11的Emulation测试的IE9和IE10,不支持IE7和IE8(IE7,IE8在设置input.type时会报错:Could not get the type property. This command is not supported.)

 

posted @ 2018-01-11 10:17  绿皮儿橘子  阅读(1078)  评论(0)    收藏  举报