检测模态对话框上的按键不起作用 [英] detect key press on modal dialog not working

查看:22
本文介绍了检测模态对话框上的按键不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();
                }
        }
    });

这篇关于检测模态对话框上的按键不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆