清除 Internet Explorer 中的文件输入框 [英] Clearing file input box in Internet Explorer

查看:35
本文介绍了清除 Internet Explorer 中的文件输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有一个文件上传框和一个清除按钮.当我按下清除按钮时,我希望文件上传框中的文本被清除.

I have an file upload box and a clear button on my page. When I press the clear button, I want the text in the file upload box to be cleared.

以下内容在 Firefox 中有效,但在 IE 中无效(文本保留在那里).有解决方法吗?

The following works in Firefox, but doesn't in IE (the text stays there). Is there a workaround for this?

$("#clear").click( function() {
    $("#attachment").val("");
    //document.myform.attachment.value = "";
})

HTML:

<form name="myform">
    <input type="file" name="attachment" id="attachment" />
</form>
<br /><button id="clear">Clear Attachment</button>

jsFiddle

推荐答案

It's readonly in IE8 以上,所以你不能清除它.绕过此安全功能的最简单方法是用副本替换元素.

It's readonly in IE8 onwards, so you can't clear it. The simplest way around this security feature is to replace the element with a copy.

编辑 找到了一个以前的答案,建议采用相同的方法!清除使用jQuery

Edit Found a previous answer to this that suggests the same approach! Clearing <input type='file' /> using jQuery

这篇关于清除 Internet Explorer 中的文件输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆