jQuery 使用 ESC 关闭引导模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16790557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:47:17  来源:igfitidea点击:

Closing bootstrap modal using ESC

javascriptjquerytwitter-bootstrap

提问by feri baco

I am using 2 modals, 1st one contains a form and 2nd shows up when an error occurs in the form. 2nd modal contains only text with error message.

我正在使用 2 个模态,第一个包含一个表单,第二个在表单中出现错误时显示。第二个模态仅包含带有错误消息的文本。

My problem is that when 2nd modal show up and I press Esc, the first one (with the form) will close instead on the 2nd one.

我的问题是,当第二个模式出现并且我按下 时Esc,第一个(带有表单)将在第二个关闭。

Is there any way how to focus the 2nd modal when it shows up?

有什么方法可以在第二个模态出现时聚焦它?

enter image description here

在此处输入图片说明

This is how it looks like, now if I pressed Esc, the 1st one would close, but I want to close the 2nd one first.

这是它的样子,现在如果我按下Esc,第一个会关闭,但我想先关闭第二个。

UPDATE

更新

Once I click somewhere on the 2nd modal, it works perfectly. I just need to select/focus it automatically

一旦我单击第二个模态上的某处,它就会完美运行。我只需要自动选择/聚焦它

回答by Mahmoude Elghandour

It looks like this is an issue with how the keyup event is being bound.

看起来这是如何绑定 keyup 事件的问题。

You can add the "tabindex"attribute to your modal to get around this issue:

您可以将“tabindex”属性添加到您的模态以解决此问题:

 tabindex="-1"

So your full code should look like this:

所以你的完整代码应该是这样的:

<a href="#my-modal" data-keyboard="true" data-toggle="modal">Open Modal</a>

<div class='modal fade hide' id='my-modal' tabindex='-1'>
<div class='modal-body'>
<div>Test</div>
</div>

For more info you can view the discussion on this issue on github: https://github.com/twitter/bootstrap/issues/4663

有关更多信息,您可以在 github 上查看有关此问题的讨论:https: //github.com/twitter/bootstrap/issues/4663