检测模态对话框上的按键不起作用

2022-01-17 00:00:00 modal-dialog jquery javascript html

我想在显示模式时检测用户是否按下了任何键.我尝试了以下代码,但未触发事件.

I would like to detect whether the user has pressed any key when a modal is shown. I tried the following code but the events are not fired.

代码片段:

   $("#modal_confirmation_dp_change").on('keydown keyup input', function ( e ) {
         alert();
        });

如果我尝试测试点击事件,它会被触发.

If I try to test click event, it is fired.

   $("#modal_confirmation_dp_change").on('click', function ( e ) {
            alert();
        });

我正在使用 twitter 引导模式.我错过了什么吗?

I am using twitter bootstrap modal. Am I missing something?

回答:我找到了解决我的问题的方法.看来我不应该指向模态的 id 以便检测到按键事件.

ANSWER: I found the solution to my problem. It seems like I should not point to the id of the modal so that the keypress event will be detected.

推荐答案

我找到了解决问题的方法.看来我不应该指向模态的 id 以便检测到按键事件.

I found the solution to my problem. It seems like I should not point to the id of the modal so that the keypress event will be detected.

$(document).on('keydown keyup input click',  function (e) {
if($('#modal_confirmation_dp_change').is(':visible')) {
            var key = e.which;
                if (key == 13) { //This is an ENTER 
                    $('#changed_dp_ok').click();
                }
        }
    });

相关文章