在JavaScript中,我可以进行“点击”事件消息以编程方式为文件输入元素? [英] In JavaScript can I make a "click" event fire programmatically for a file input element?

查看:88
本文介绍了在JavaScript中,我可以进行“点击”事件消息以编程方式为文件输入元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以编程方式在< code>< / code>标记上点击事件。



只是调用click()似乎没有做任何事情,或者至少它不会弹出文件选择对话框。



我已经一直在尝试使用监听器捕获事件并重定向事件,但是我一直没有能够实际执行事件,就像有人点击它一样。

解决方案

你不能在所有浏览器中这样做,据说IE 确实允许它,但Mozilla和Opera不支持。

当您在GMail中编写邮件时,附件功能以IE和任何支持此功能的浏览器的方式实现,然后以Firefox和那些没有的浏览器。



我不知道为什么你不能这么做,但有一点是 存在安全风险,您不允许在任何浏览器中执行操作,以编程方式在HTML File元素上设置文件名。


I'd like to make a click event fire on an <input type="file"> tag programmatically.

Just calling click() doesn't seem to do anything or at least it doesn't pop up a file selection dialog.

I've been experimenting with capturing events using listeners and redirecting the event, but I haven't been able to get that to actually perform the event like someone clicked on it.

解决方案

You cannot do that in all browsers, supposedly IE does allow it, but Mozilla and Opera do not.

When you compose a message in GMail, the 'attach files' feature is implemented one way for IE and any browser that supports this, and then implemented another way for Firefox and those browsers that do not.

I don't know why you cannot do it, but one thing that is a security risk, and which you are not allowed to do in any browser, is programmatically set the file name on the HTML File element.

这篇关于在JavaScript中,我可以进行“点击”事件消息以编程方式为文件输入元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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