是否有可能清除<input type='file' />控件值与jQuery?我试过以下几种方法:

$('#control').attr({ value: '' }); 

但这并不奏效。


当前回答

这适用于Chrome, FF和Safari

$("#control").val("")

可能无法与IE或Opera工作

其他回答

在IE8中,为了安全起见,他们将文件上传字段设置为只读。查看IE团队的博客文章:

Historically, the HTML File Upload Control () has been the source of a significant number of information disclosure vulnerabilities. To resolve these issues, two changes were made to the behavior of the control. To block attacks that rely on “stealing” keystrokes to surreptitiously trick the user into typing a local file path into the control, the File Path edit box is now read-only. The user must explicitly select a file for upload using the File Browse dialog. Additionally, the “Include local directory path when uploading files” URLAction has been set to "Disable" for the Internet Zone. This change prevents leakage of potentially sensitive local file-system information to the Internet. For instance, rather than submitting the full path C:\users\ericlaw\documents\secret\image.png, Internet Explorer 8 will now submit only the filename image.png.

一个简单的方法是改变输入类型,然后再把它改回来。

就像这样:

var input = $('#attachments');
input.prop('type', 'text');
input.prop('type', 'file')

什么? 在验证函数中

document.onlyform.upload.value="";

假设upload是名称:

<input type="file" name="upload" id="csv_doc"/>

我使用JSP,不确定这是否有区别…

对我来说是可行的,而且我觉得这样简单多了。

我被这里的选项困住了。下面是我做的一个有效的方法:

<form>
 <input type="file">
 <button type="reset" id="file_reset" style="display:none">
</form>

你可以使用jQuery来触发重置,代码如下:

$('#file_reset').trigger('click');

(JSFIDDLE: http://jsfiddle.net/eCbd6/)

文件输入的值是只读的(出于安全原因)。不能通过编程方式使其变为空白(除非调用表单的reset()方法,该方法的范围比该字段更广)。