我正在用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/*" />
我该怎么做呢?
当前回答
特别是对于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
其他回答
jQuery的解决方案:
$('input').on('change',function(){
$(this).get(0).value = '';
$(this).get(0).type = '';
$(this).get(0).type = 'file';
});
你不能重置,因为它是一个只读文件。你可以克隆它来解决这个问题。
你可以克隆它,用它自己替换它,所有事件仍然附加:
<input type="file" id="control">
and
var input = $("#control");
function something_happens() {
input.replaceWith(input.val('').clone(true));
};
谢谢:csstricks.com
有许多方法,我将建议去参考附件输入。
<input
id="image"
type="file"
required
ref={ref => this.fileInput = ref}
multiple
onChange={this.onFileChangeHandler}
/>
在提交后清除值。
this.fileInput.value = "";
重置输入文件是非常单一的
$('input[type=file]').val(null);
如果你绑定重置文件中更改表单的其他字段,或者用ajax加载表单。
本例适用
选择器,例如$('input[type=text]')或表单的任何元素
事件单击,更改,或任何事件
$('body').delegate('event', 'selector', function(){
$('input[type=file]').val(null) ;
});