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

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

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

我该怎么做呢?


当前回答

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

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

我用:

  file_input.files=new DataTransfer().files  

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

其他回答

<input type="file" id="mfile" name="mfile">
<p>Reset</p>

<script>
$(document).ready(function(){
$("p").click(function(){

            $("#mfile").val('');
            return false;

    });
});

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

你需要将<input type = " file " >包装成<form>标签,然后你可以通过重置你的表单来重置输入:

onClick="this.form.reset()"

解决方案

下面的代码是用jQuery编写的。它适用于所有浏览器,并允许保存事件和自定义属性。

var $el = $('#uploadCaptureInputFile');
$el.wrap('<form>').closest('form').get(0).reset();
$el.unwrap();

DEMO

有关代码和演示,请参阅jsFiddle。

链接

有关更多信息,请参见如何使用JavaScript重置文件输入。

你可以克隆它,用它自己替换它,所有事件仍然附加:

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

and

var input = $("#control");

function something_happens() {
    input.replaceWith(input.val('').clone(true));
};

谢谢:csstricks.com