1

我的 jquery 对话框上有两个按钮确定并取消。我希望当用户在当前打开的 Jquery ui 对话框上按 Enter 键时,它的行为就像单击 Ok 一样。按钮定义如下:

     $("#dialog-confirm").dialog({
                    autoOpen: true,
                    resizable: true,
                    modal: true,
                    width: 458,
                    Height: 184,

                    buttons: {
                        "Login": function () {
                            $(this).dialog("close");  
 $("#frmsetAccount").submit();                        
                        },
                        "Cancel": function () {
                            $(this).dialog("close");                           

                        }
                    }
                });
    });

请提出解决方案

4

3 回答 3

3

您可以使用 KeyPress 或 KeyUp 事件

$('#targetElement').on('keypress', function(e) {
 var code = (e.keyCode ? e.keyCode : e.which);
 if(code == 13) { //Enter keycode
   //close your dialog box. -make sure you get the dialog object first. then close it. or call some function which is in ok button. 
 }
});

键位

$('#targetElement').keyup(function(e) {
     var code = (e.keyCode ? e.keyCode : e.which);
     if(code == 13) { //Enter keycode
       //Do something
     }
    });
于 2013-02-14T12:05:32.327 回答
2

我意识到这篇文章很旧,但这些回复并没有真正回答这个问题。如果您正确设置对话框,则向 jQuery 对话框添加热键事件很简单。我在下面写了一些代码,这些代码对于大多数用例来说都是通用的。

您可以在https://jsfiddle.net/bsalines/xpvt214o/3180/查看工作副本

$('<div id="yourJqueryDialogId"></div>').appendTo('body')
    .html(html) //add your custom html to be displayed in dialog
    .dialog({
        modal: true,
        title: 'Some Title',
        autoOpen: true,
        width: 500,
        height: 450,
        resizable: false,
        draggable: false,

        buttons: {
            Cancel: {
                text: "Cancel",
                class: "model-cancel",
                click: function() {
                    // Add your code if you want to do something on close/cancel/exit
                    $(this).dialog("close");
                    $(this).dialog('destroy').remove();
                }
            },
            Save: {
                text: "Update",
                class: "model-update",
                id: "yourJqueryButtonId", // id for hotkey
                click: function() {
                    //Add your code here for hotkey and update/save/submit
                    $(this).dialog("close");
                    $(this).dialog('destroy').remove();

                },

            },
        }
    });

$("#yourJqueryDialogId").keydown(function(e) { if(e.which == 13) {
    $('#yourJqueryButtonId').click();
           return false;
      } });
于 2018-03-29T14:27:04.160 回答
0

只需使用keyup

$(document).keyup(function(e) {
    if (e.keyCode == 13) // close the dialog
});
于 2013-02-14T12:05:27.460 回答