$('#id').click();

它不适用于Mac OS上的Chrome 26.

The problem actually is creation "upload" widget that can be integrated in a form. Widget will consists of two parts. The first part is div with initiator button and error/success messages. I think the way is put another form as the second part with file input and submit file into the iframe. After submition we fill hidden field in first part in main form or show errors in the same.

Easy way is adding file-form into main-form, but it's prohibited.

推荐答案

使用jQuery

我会创建一个按钮和一个不可见的输入,如下所示:

<button id="button">Open</button>
<input id="file-input" type="file" name="name" style="display: none;" />

and add some jQuery to trigger it:

$('#button').on('click', function() {
    $('#file-input').trigger('click');
});

使用香草JS

相同的 idea ,没有jQuery(@Pascale分):

<button onclick="document.getElementById('file-input').click();">Open</button>
<input id="file-input" type="file" name="name" style="display: none;" />

Jquery相关问答推荐

如果文本框内容在 X 秒内没有更改,则进行 post 调用

在函数 jQuery 中更改语言

加载外部 css 文件,如 jquery 中的脚本,这在 ie 中也兼容

在 CSS3 或 jQuery 中将元素的宽度从 0% 设置为 100% 动画,它和它的包装器只需要它们需要的宽度,没有预先设置的宽度

使用 jQuery 的并行异步 Ajax 请求

错误找不到 jquery-2.0.2.min.map

如何使用Angular 检测浏览器后退按钮单击事件?

如何使用 JQuery 发布 JSON 数据?

延迟后运行功能

如何使div全屏?

Jquery - 如何获取样式显示属性无/块

使用 jQuery 拖放防止单击事件

如何在javascript中获取json键和值?

如何使用 jQuery 停止默认链接点击行为

更改 Eclipse 设置以忽略特定文件上的错误

如何在页面加载时启动 jQuery Fancybox?

jQuery attr 与props ?

使用 jQuery 在 Select 列表中隐藏选项

添加到数组 jQuery

如何在 jquery 中获取 textarea 的值?