detect key press on modal dialog not working

前端 未结 3 1246
离开以前
离开以前 2020-12-20 04:34

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.

Code snippet:

         


        
3条回答
  •  甜味超标
    2020-12-20 04:55

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

提交回复
热议问题