对话框小部件。当我按下 return 键时如何以编程方式调用 ok 方法

Dialog Widget. How do I programmatically call the ok method when I hit return key

我正在使用 jQuery UI 对话框小部件。我有一个与按钮对象中的 "ok" 键关联的函数:

var myself = this;

    this.dialogForm.dialog({
                autoOpen: false,
                modal: true,
                buttons: {
                    "ok": ok,
                    cancel: function() {
                        myself.dialogForm.dialog( "close" );
                    }
                },
                close: function() {
                    myself.form[ 0 ].reset();
                    myself.dialogForm.remove();
                    myself.dialogForm = undefined;
                    if (trackMenu) trackMenu.hide();
                }
            });

如何在按下 return 键时以编程方式调用 ok 函数。我想摆脱用户将鼠标悬停并单击确定按钮的需要。

在 jQuery UI 模式打开时添加一个事件侦听器。

非常 基本示例:

function modalEnterKeyPress (e) {
    if (e.keyCode === 13) 
        return ok()    
}

// When you open the modal
$(document.body).one('keyup', modalEnterKeyPress);

// When you close the modal
$(document.body).off('keyup', modalEnterKeyPress);