1

jqueryダイアログに2つのボタンがあります[OK]と[キャンセル]。ユーザーが現在開いているJqueryuiダイアログで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

キーアップを使用するだけです

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