我正在用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
其他回答
这是最好的解决方案:
input.value = ''
if(!/safari/i.test(navigator.userAgent)){
input.type = ''
input.type = 'file'
}
在所有的答案中这是最快的解。没有输入克隆,没有窗体重置!
我在所有浏览器中都检查了它(它甚至支持IE8)。
<input type="file" id="mfile" name="mfile">
<p>Reset</p>
<script>
$(document).ready(function(){
$("p").click(function(){
$("#mfile").val('');
return false;
});
});
你应该试试这个:
$("#uploadCaptureInputFile").val('');
@dhaval-marthak在评论中发布的jQuery解决方案显然是有效的,但如果你看看实际的jQuery调用,就很容易看出jQuery在做什么,只是将value属性设置为空字符串。所以在“纯”JavaScript中,它将是:
document.getElementById("uploadCaptureInputFile").value = "";
getElementById (uploadCaptureInputFile”)的文件。价值;