以编程方式触发“选择文件”对话框 [英] Programmatically trigger "select file" dialog box

查看:109
本文介绍了以编程方式触发“选择文件”对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个隐藏文件输入元素。是否有可能通过按钮的点击事件来触发它的 select file 对话框?

I have a hidden file input element. Is it possible to trigger its select file dialog box from a button's click event?

推荐答案

'您正在寻找自己的按钮来上传文件而不是使用< input type =file/> ,您可以执行以下操作:

If you're looking to have your own button to upload a file instead of using <input type="file" />, you can do something like:

<input id="myInput" type="file" style="visibility:hidden" />
<input type="button" value="Show Dialog" onclick="$('#myInput').click();" />

请注意,我使用 visibility:hidden ,而不是 display:none 。您无法在未显示的文件输入中调用点击事件。

Note that I used visibility: hidden, instead of display: none. You cannot call the click event on a non-displayed file input.

这篇关于以编程方式触发“选择文件”对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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