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

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

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

我该怎么做呢?


当前回答

我用来做vuejs

   <input
          type="file"
          ref="fileref"
          @change="onChange"/>

this.$refs.fileref.value = ''

其他回答

函数重置图像字段() { var $el = $('#uploadCaptureInputFile'); $el.wrap('<form>').closest('form').get(0).reset(); $el.解包(); } <script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script> <input type=“file” id=“uploadCaptureInputFile” class=“win-content colors” accept=“image/*” /> <按钮点击=“重置图像字段()”>重置字段</button>

可以这样做

var inputfile= $('#uploadCaptureInputFile') $('#reset').on('click',function(){ inputfile.replaceWith(inputfile.val('').clone(true)); }) <script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script> <input type=“file” id=“uploadCaptureInputFile” class=“win-content colors” accept=“image/*” /> <a href=“” id=“reset”>重置</a>

特别是对于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 type="file" id="fileControl">

然后就这样做:

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

重置文件控件。

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

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