我正在用VS2012和Javascript开发一个地铁应用程序
我想重置我的文件输入的内容:
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
我该怎么做呢?
我正在用VS2012和Javascript开发一个地铁应用程序
我想重置我的文件输入的内容:
<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />
我该怎么做呢?
当前回答
@dhaval-marthak在评论中发布的jQuery解决方案显然是有效的,但如果你看看实际的jQuery调用,就很容易看出jQuery在做什么,只是将value属性设置为空字符串。所以在“纯”JavaScript中,它将是:
document.getElementById("uploadCaptureInputFile").value = "";
其他回答
也适用于动态控制。
Javascript
<input type="file" onchange="FileValidate(this)" />
function FileValidate(object) {
if ((object.files[0].size / 1024 / 1024) > 1) { //greater than 1 MB
$(object).val('');
}
}
JQuery
<input type="file" class="UpoloadFileSize">
$(document).ready(function () {
$('.UpoloadFileSize').bind('change', function () {
if ((this.files[0].size / 1024000) > 1.5) { //greater than 1.5 MB
$(this).val('');
alert('Size exceeded !!');
}
});
});
在IE 10中,我解决了这个问题:
var file = document.getElementById("file-input");
file.removeAttribute('value');
file.parentNode.replaceChild(file.cloneNode(true),file);
地点:
<input accept="image/*" onchange="angular.element(this).scope().uploadFile(this)" id="file-input" type="file"/>
如果你的表单中有几个文件,但只想重置其中一个:
如果你使用boostrap, jquery, filstyle来显示输入文件的形式:
$("#"+idInput).filestyle('clear');
getElementById (uploadCaptureInputFile”)的文件。价值;
重置文件上传按钮是如此容易使用纯JavaScript!
有几个方法可以做到这一点,但必须直接的方法,这是在许多浏览器工作良好的改变文件值为零,这样上传文件得到重置,也尝试使用纯javascript而不是任何框架,我创建了下面的代码,检查一下(ES6):
函数resetFile() { const file = document.querySelector('.file'); 文件。Value = "; } <input type="file" class="file" /> <按钮onclick = " resetFile ()“>重置文件> < /按钮