Dialog keypress and DOM

 ̄綄美尐妖づ 提交于 2019-12-10 00:20:06

问题


I'm trying to use jQuery's keypress to trigger a button click on a modal dialog created using the jQuery dialog function. The problem is, with the following code, it works the first time around (pressing enter presses the Save button) but I get erratic behavior when I close the modal dialog and reopen it. I'm thinking some variant of $(this).("button:contains('Save')") would work, but that doesn't work.

$('#dialog').keypress(function(e) {
        if (e.which == 13) {
      $("button:contains('Save')").click();
        }
});

FYI the dialog is opened using a $("#dialog").dialog('open'), not an autoOpen:true. What would be the best practice for the task?

Thanks!


回答1:


I would do:

$('#dialog').keyup(function(e) {
    if (e.which == 13) {
         var buttons = $(this).dialog('option', 'buttons');
         buttons['Save']();
         e.stopPropagation();
    }
})



回答2:


keypress is for letters. keydown is for everything. I would try using keydown.

Your code most likely does not execute. Enter will trigger the default action so that's why it works the first time.

More info here:

http://www.bloggingdeveloper.com/post/KeyPress-KeyDown-KeyUp-The-Difference-Between-Javascript-Key-Events.aspx



来源:https://stackoverflow.com/questions/1539758/dialog-keypress-and-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!