我正在用VS2012和Javascript开发一个地铁应用程序

我想重置我的文件输入的内容:

<input type="file" id="uploadCaptureInputFile" class="win-content colors" accept="image/*" />

我该怎么做呢?


当前回答

如果你有以下情况:

<input type="file" id="fileControl">

然后就这样做:

$("#fileControl").val('');

重置文件控件。

其他回答

你需要将<input type = " file " >包装成<form>标签,然后你可以通过重置你的表单来重置输入:

onClick="this.form.reset()"

特别是对于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

@dhaval-marthak在评论中发布的jQuery解决方案显然是有效的,但如果你看看实际的jQuery调用,就很容易看出jQuery在做什么,只是将value属性设置为空字符串。所以在“纯”JavaScript中,它将是:

document.getElementById("uploadCaptureInputFile").value = "";

在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');