简体   繁体   English

使用ESC关闭bootstrap模式

[英]Closing bootstrap modal using ESC

I am using 2 modals, 1st one contains a form and 2nd shows up when an error occurs in the form. 我正在使用2个模态,第一个包含一个表单,第二个在表单中发生错误时显示。 2nd modal contains only text with error message. 第二个模态仅包含带有错误消息的文本。

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? 有没有办法在第二个模态显示时如何聚焦?

在此输入图像描述

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 UPDATE

Once I click somewhere on the 2nd modal, it works perfectly. 一旦我点击第二个模态的某个地方,它就完美无缺。 I just need to select/focus it automatically 我只需要自动选择/聚焦它

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

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

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