jQuery:模拟对 <input type="file" 的点击/>在 Firefox 中不起作用? [英] jQuery : simulating a click on a <input type="file" /> doesn't work in Firefox?

查看:33
本文介绍了jQuery:模拟对 <input type="file" 的点击/>在 Firefox 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
在 JavaScript 中我可以做一个点击"以编程方式为文件输入元素触发事件?

我有一个看起来像这样的网页

I've got a web page that looks like this

<html>
    <head>
        <title>File Upload Click Test</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    </head>
    <body>
        <div onclick="$('input[type=file]').click()" >CLICK SIMULATOR</div>
        <input type="file"></input>
    </body>
</html>

我的目标是让 divfile input 上引发一个点击事件,这似乎与我在 IE 和 Chrome 中所期望的完全一样,但是在 Firefox 中不起作用(单击 div 时不会打开文件浏览器).

My goal is to have the div raise a click event on the file input, and this seems to work exactly as I'd expect in IE and Chrome, but doesn't work in Firefox (no file browser is opened when you click on the div).

有没有办法让它在 FF 中工作?

Is there a way to get this working in FF?

推荐答案

有没有办法让它在 FF 中工作?

Is there a way to get this working in FF?

不,它也不适用于大多数常见版本的 IE.IE 将打开该对话框,但一旦您选择了包含该对话框的文件,该表单将不会实际提交.

No, and it doesn't work in most common versions of IE, either. IE will open the dialog, but once you've selected a file with it the form won't actually submit.

放弃希望.伪造文件上传框的唯一方法是使用透明技术,这真的不推荐,因为浏览器可能会在内部以不同的方式布置文件上传框(甚至提供不包含浏览对话框的文件上传控件),很可能你最终会得到一个无法操作的表格.

Abandon hope. The only way to fake a file upload box is using the transparency technique, and that's really not recommended at all as browsers may lay out file upload boxes differently internally (or even provide a file upload control that doesn't include a Browse dialogue), making it highly likely you'll end up with an inoperable form.

学会喜欢灰色的文件上传字段,或使用渐进增强功能将其替换为可用的 Flash.

Learn to love the grey file upload field, or use progressive enhancement to replace it with Flash where available.

这篇关于jQuery:模拟对 &lt;input type="file" 的点击/&gt;在 Firefox 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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