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

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

问题描述


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


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

 < html> 
< head>
< title>文件上传点击测试< /标题>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js>< / script>
< / head>
< body>
< div onclick =$('input [type = file]')。click()>点击模拟器< / div>
< input type =file>< / input>
< / body>
< / html>

我的目标是让 div em>文件输入,并且这看起来像我在IE和Chrome期望的那样工作,但是在Firefox中不起作用(当你点击 div

有没有一种方法可以在FF中使用这个工作?

解决方案


有没有办法让这个工作在FF?


没有,它doesn在大多数常见的IE版本中都不行。 IE会打开对话框,但一旦你选择了一个文件,表单实际上不会提交。



放弃希望。假冒一个文件上传框的唯一方法是使用透明技术,这实际上是不推荐的,因为浏览器可能在内部以不同的方式布置文件上传盒(或者甚至提供不包括浏览对话的文件上传控件),这使得它很有可能会以不可操作的形式出现。



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

Possible Duplicate:
In JavaScript can I make a “click” event fire programmatically for a file input element?

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>

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).

Is there a way to get this working in FF?

解决方案

Is there a way to get this working in FF?

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.

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

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

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