如何在IE中清除选定文件
以下示例适用于Chrome,但不适用于任何版本的IE
HTML
<input id="file1" type="file" class="" multiple="" required="" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
Jquery
$("#fileUpload").val('');
正如预期的那样,IE不支持此功能。
如何在IE中清除选定文件
以下示例适用于Chrome,但不适用于任何版本的IE
HTML
<input id="file1" type="file" class="" multiple="" required="" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
Jquery
$("#fileUpload").val('');
正如预期的那样,IE不支持此功能。
输入文件列表是只读的,所以您无法从中删除任何文件,这就是为什么IE不支持清除输入值的原因。
你可以使用以下方法解决IE的问题
$("#image").remove("");
$("form").append(' <input id="image" type="file" name="image"/>');
function reset_form_element(e) {
e.wrap('<form>').parent('form').trigger('reset');
e.unwrap();
}
reset_form_element($('#file1'));
$.fn.extend({
clearFiles: function () {
$(this).each(function () {
var isIE = (window.navigator.userAgent.indexOf("MSIE ") > 0 || !! navigator.userAgent.match(/Trident.*rv\:11\./));
if ($(this).prop("type") == 'file') {
if (isIE == true) {
$(this).replaceWith($(this).val('').clone(true));
} else {
$(this).val("");
}
}
});
return this;
}
});
Use:
$('#test').click(function () {
$("[type='file']").clearFiles();
});
Select Photo(optional): <span id="form_sp"><input id="file" type="file" size="40" name="photo"/></span> <button id="reset_t">Reset file</button>
<script>
$(document).ready(function(){
$('#reset_t').click(function(e) {
e.preventDefault(); // keeps form from submitting
$("#form_sp").html('');
$("#form_sp").html('<input id="file" type="file" size="40" name="photo"/>');
});
});
</script>