我有一个页面( demo ):
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function() {
$("#id_button").click(function(e) {
$("#id_file").click();
});
});
</script>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="http://www.google.com/">
<input type="file" name="file" id="id_file" />
<input type="button" id="id_button" value="fake button" />
<input type="submit">
</form>
</body>
</html>
如果
我通过单击“假按钮”打开浏览对话框,选择文件(我在 input[type="file"]
中看到它),然后单击提交按钮,并且没有发生任何帖子,input[ type="file"]
已清除。
我应该改变什么才能让它工作?
我在 IE8 和 IE10 中遇到此问题。
PS:文件输入将被隐藏,因此用户只能使用假按钮。
最佳答案
所有浏览器在允许您通过 javascript 以编程方式单击文件输入的输入按钮时都有不同的行为。
我发现似乎可以跨浏览器工作的最佳解决方案是将不透明度设置为 0(不要使用 display:none)并将按钮放在输入下方,以便用户通过 0 不透明度输入单击按钮,从而触发输入选择对话框。
可以在这里找到有关文件输入样式的精彩文章:http://www.quirksmode.org/dom/inputfile.html
关于javascript - IE 在提交时清除输入[类型 ="file"],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18874861/