【问题标题】:Open a file dialog when parent div is clicked using jQuery使用 jQuery 单击父 div 时打开文件对话框
【发布时间】:2012-12-16 16:59:07
【问题描述】:

我想在单击父 div 时打开一个文件对话框。如果我单击第一个父 div,它应该只打开第一个输入文件。

<div class="fileupload">
    <input type="file" class="file" name="image" />
</div>

<div class="fileupload">
    <input type="file" class="file" name="attachement" />
</div>

【问题讨论】:

标签: jquery file input


【解决方案1】:

只需在input 元素上触发click 事件:

$('.fileupload').click(function(e) {
    $(this).find('input[type="file"]').click();
});

$('.fileupload input').click(function(e) {
    e.stopPropagation();
});​

演示:http://jsfiddle.net/EctCK/

【讨论】:

  • 感谢您的回复,当我尝试此操作时,我收到此错误 'Uncaught SyntaxError: Unexpected token }'
  • @user1915190:这是脚本中某处的语法错误。
  • 当我点击 div 时,我的浏览器崩溃了
  • 我的情况有点不同,但这个答案确实帮助我塑造了我的代码。
【解决方案2】:

尝试使用trigger()

$(document).ready(function() {
    $(this).parents(".fileupload").find("input[type='file']").trigger('click');
});

【讨论】:

  • 谢谢,当我点击父 div 时,我的浏览器崩溃了
  • 再试一次,用其他浏览器试试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多