由于 javascript 不能清除 input:file 上传控件的值,因此最好的方法是在 input:file 上传控件的外层嵌入 <form> 元素,使用 <form> 元素的 reset() 方法来清除input:file 上传控件的值。代码如下:
function clearFileInput(file) {
var form = document.createElement('form');
document.body.appendChild(form);
var pos = file.nextSibling;
form.appendChild(file);
form.reset();
pos.parentNode.insertBefore(file, pos);
document.body.removeChild(form);
}

本文介绍了一种通过在外层嵌套<form>元素并利用其reset()方法来清空HTML中input:file控件值的技术。这种方法绕过了JavaScript无法直接清除input:file值的限制。
1675

被折叠的 条评论
为什么被折叠?



