我正在用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/*" />
我该怎么做呢?
当前回答
你需要将<input type = " file " >包装成<form>标签,然后你可以通过重置你的表单来重置输入:
onClick="this.form.reset()"
其他回答
也适用于动态控制。
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 !!');
}
});
});
@dhaval-marthak在评论中发布的jQuery解决方案显然是有效的,但如果你看看实际的jQuery调用,就很容易看出jQuery在做什么,只是将value属性设置为空字符串。所以在“纯”JavaScript中,它将是:
document.getElementById("uploadCaptureInputFile").value = "";
我在这里错过了另一个解决方案(2021年): 我使用FileList与文件输入交互。
因为没有办法创建FileList对象,所以我使用DataTransfer,它带来了干净的FilleList。
我用:
file_input.files=new DataTransfer().files
在我的例子中,这非常适合,因为我只通过FileList与封装的文件输入元素进行交互。
你可以克隆它,用它自己替换它,所有事件仍然附加:
<input type="file" id="control">
and
var input = $("#control");
function something_happens() {
input.replaceWith(input.val('').clone(true));
};
谢谢:csstricks.com
特别是对于Angular来说
document.getElementById('uploadFile').value = "";
会工作,但是如果你使用Angular呢 它会说“属性‘value’在类型‘HTMLElement’.any上不存在”
document.getElementById()返回不包含value属性的HTMLElement类型。因此,将它转换为HTMLInputElement
(<HTMLInputElement>document.getElementById('uploadFile')).value = "";
额外的:-
然而,我们不应该在angular中使用DOM操作(document.xyz())。
为此,angular提供了@ViewChild、@ViewChildren等,分别是document.querySelector()、document.queryselectorAll()。
连我都没读过。最好关注专家的博客
通过这个link1和link2