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

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

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

我该怎么做呢?


当前回答

使用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试样

其他回答

重置输入文件是非常单一的

$('input[type=file]').val(null);

如果你绑定重置文件中更改表单的其他字段,或者用ajax加载表单。

本例适用

选择器,例如$('input[type=text]')或表单的任何元素

事件单击,更改,或任何事件

$('body').delegate('event', 'selector', function(){
     $('input[type=file]').val(null) ;
});

如果你有以下情况:

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

然后就这样做:

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

重置文件控件。

getElementById (uploadCaptureInputFile”)的文件。价值;

只使用:

$('input[type=file]').val('');

jQuery的解决方案:

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