简体   繁体   English

在“打开文件”对话框上按Escape键会触发JavaScript键按下

[英]Pressing Escape Key on Open File Dialog Fires a JavaScript Key Press

I have an HTML dialog set to remove itself when the escape key is pressed. 我设置了一个HTML对话框,以便在按Escape键时将其自身删除。 I have a file field in the HTML dialog. 我在HTML对话框中有一个文件字段。 If a user is using the system file dialog to select a file for the file field in the HTML dialog and presses escape, that event closes the system dialog and also passes the escape key event to the browser, which also closes the HTML dialog. 如果用户使用系统文件对话框为HTML对话框中的file字段选择文件,然后按Escape键,则该事件将关闭系统对话框,并且还将Escape键事件传递给浏览器,浏览器也会关闭HTML对话框。 I want the HTML dialog to stay open, and don't want that event to hit the browser. 我希望HTML对话框保持打开状态,并且不希望该事件触发浏览器。

How can I fix this? 我怎样才能解决这个问题?

More info: This behavior is in Firefox. 更多信息:此行为在Firefox中。

If you are using the jQuery uI dialog, you could check for the escape key in the beforeClose event, and use e.preventDefault to stop it from closing: 如果使用的是jQuery uI对话框,则可以在beforeClose事件中检查转义键,然后使用e.preventDefault使其停止关闭:

$( ".selector" ).dialog({
   beforeClose: function(event, ui) {
                 if(event.keyCode == 27) {  
                     alert("Esc key tried to close the dialog");
                     event.preventDefault();
                 } else {  
                     alert("You used something other than the escape key to close the dialog.");  
                 }
   }
});

Only drawback is if you want to allow closing the dialog with the escape key when the system file dialog is NOT open, this will prevent that from working. 唯一的缺点是,如果您希望在未打开系统文件对话框的情况下允许使用转义键关闭对话框,则这将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM