javascript - IE 在提交时清除输入[类型 ="file"]

标签 javascript html internet-explorer

我有一个页面( 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/

相关文章:

javascript - 下拉从列表中删除空白项

javascript - Dojo TabContainer 没有样式

javascript - 在十字图标和 HTML 正文单击上禁用花式框关闭功能

javascript - AJAX 成功不适用于 IE 和 Firefox

javascript - 用图像替换文本会调用 "IE has restricted this page from running scripts or ActiveX"

javascript - 如何在 React Native 中为不同的 IOS 设备设置字体大小

javascript - 处理extjs收到数据后存储代理数据

html - 如何让 TABLE 增长到与其容器相同的高度? (或者,与 IE JavaScript 缓慢作斗争)

html - 从 R 中的许多 html 文件创建语料库

html - 即使 IE=edge,兼容模式按钮仍然存在