我正在用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 = ''

其他回答

我用来做vuejs

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

this.$refs.fileref.value = ''

如果你有以下情况:

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

然后就这样做:

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

重置文件控件。

jQuery的解决方案:

    $('input').on('change',function(){
                $(this).get(0).value = '';
                $(this).get(0).type = '';
                $(this).get(0).type = 'file';
            });

使用angular,你可以创建一个模板变量,并将其值设置为null

<input type="file" #fileUploader />
<button (click)="fileUploader.value = null">Reset from UI</button>

或者你可以像这样创建一个方法来重置

component.ts

  @ViewChild('fileUploader') fileUploader:ElementRef;

  resetFileUploader() { 
    this.fileUploader.nativeElement.value = null;
  }

模板

<input type="file"/>
<button (click)="resetFileUploader()">Reset from Component</button>

stackblitz试样

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