显示浏览器的“选择文件”;通过键盘导航聚焦输入[类型=文件]时的对话框

发布于 2024-08-11 07:44:46 字数 1078 浏览 2 评论 0原文

正如标题所示,我希望当某个输入通过在表单字段中切换(使用键盘导航)获得焦点时打开“选择文件”对话框。默认情况下,仅当单击该字段时才会打开“选择文件”窗口。

我在 JS Bin 上针对这个问题建立了一个页面: http://jsbin.com/areba/edit

目前,该页面由以下代码组成:

<!doctype html> 
<html> 
 <head> 
  <title>Sandbox</title> 
  <meta charset="utf-8"> 
 </head> 
 <body> 
  <form> 
   <input type="text"> 
   <input type="file">
  </form>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
  <script>
   $(function() {
    $('input[type=text]').focus(function() {
     $(this).next('input[type=file]').css('background', 'lime').trigger('click');
    });
   });
  </script>
 </body> 
</html>

如您所见,有一个文本输入和一个文件输入。这个想法是,当文本输入接收焦点时,文件输入被“单击”或其他任何操作,并且“选择文件”窗口打开。

.css('background', 'lime') 语句似乎工作正常;但是,在文件输入上调用 .trigger('click') 似乎根本不起作用。

(我意识到这可能会导致可访问性问题,所以请不要讨论这个问题。谢谢。)

As the title says, I want the ‘select file’ dialog to open when a certain input gets focus by tabbing through the form fields (using keyboard navigation). By default, the ‘select file’ window only opens when the field is clicked.

I put up a page on JS Bin for this issue: http://jsbin.com/areba/edit

Currently, this page consists of the following code:

<!doctype html> 
<html> 
 <head> 
  <title>Sandbox</title> 
  <meta charset="utf-8"> 
 </head> 
 <body> 
  <form> 
   <input type="text"> 
   <input type="file">
  </form>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
  <script>
   $(function() {
    $('input[type=text]').focus(function() {
     $(this).next('input[type=file]').css('background', 'lime').trigger('click');
    });
   });
  </script>
 </body> 
</html>

As you can see, there is a text input and a file input. The idea is that when the text input receives focus, the file input gets ‘clicked’ or whatever and the ‘select file’ window opens.

The .css('background', 'lime') statement seems to work fine; however, invoking .trigger('click') on the file input seems to do nothing at all.

(I realize this might cause an accessibility problem, so please, let’s not discuss that. Thanks.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2024-08-18 07:44:46

这将在 IE 和 Safari 中工作(我认为),但在 Opera 或 Firefox 中不起作用,因为它们还没有实现文件上传元素的 click() 事件 - 编辑:还没有。

This will work in IE and Safari (I think), but not in Opera or Firefox, as they haven't implemented the click() event for file upload elements - edit: yet.

泛滥成性 2024-08-18 07:44:46

出于安全原因,我怀疑您是否能够激活该对话框。真正的点击事件必须发生,具体取决于浏览器。

我知道Flash/Flex有这个要求。

I doubt you would be able to do activate the dialog for security reasons. A real click event has to happen, depending on the browser.

I know Flash/Flex have this requirement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文