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

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

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

我该怎么做呢?


当前回答

这是最好的解决方案:

input.value = ''

if(!/safari/i.test(navigator.userAgent)){
  input.type = ''
  input.type = 'file'
}

在所有的答案中这是最快的解。没有输入克隆,没有窗体重置!

我在所有浏览器中都检查了它(它甚至支持IE8)。

其他回答

你不能重置,因为它是一个只读文件。你可以克隆它来解决这个问题。

函数重置图像字段() { 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>

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

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

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

本例适用

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

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

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

在IE 10中,我解决了这个问题:

    var file = document.getElementById("file-input");
    file.removeAttribute('value');
    file.parentNode.replaceChild(file.cloneNode(true),file);

地点:

<input accept="image/*" onchange="angular.element(this).scope().uploadFile(this)" id="file-input" type="file"/>

我在这里错过了另一个解决方案(2021年): 我使用FileList与文件输入交互。

因为没有办法创建FileList对象,所以我使用DataTransfer,它带来了干净的FilleList。

我用:

  file_input.files=new DataTransfer().files  

在我的例子中,这非常适合,因为我只通过FileList与封装的文件输入元素进行交互。