JQuery Dialog OK按钮加载时的焦点

时间:2013-08-14 10:15:56

标签: jquery jquery-dialog

我正在使用JQuery UI对话框来验证警报消息,它工作正常。但我有一个焦点问题如下。

当我检查验证并关注复选框时,现在我弹出对话框警告,然后焦点出现在对话框弹出窗口的“OK”上,它运行正常。

但是当我的重点放在文本框上时,现在我弹出对话框警告,然后焦点不会出现在对话框弹出窗口的“OK”上。

代码:

$("#dialog-message").dialog({
    modal: true,
    height: 160,
    width: 350,
    //closeOnEscape: false, 
    buttons: {
        Ok: function () {
            $(this).dialog("close");
            if (focusElement != null && focusElement != "") {
                document.getElementById(focusElement).focus();
            }
        }
    },
    open: function () {
    }});

因此,当我发出警告对话框并且我的重点是文本框时,我想将焦点设置为OK。

2 个答案:

答案 0 :(得分:1)

您可以尝试将此焦点设置为加载时的OK按钮

open: function () {
    $(this).parent().find('button:nth-child(1)').focus();
}

DEMO.

答案 1 :(得分:1)

在按钮上添加一个id,并在对话框打开时聚焦按钮:

$("#dialog-message").dialog({
    modal: true,
    height: 160,
    width: 350,
    //closeOnEscape: false, 
    buttons: [{
        id: "close-button",            // <-- set the id
        text: "OK",
        click: function() {
            $(this).dialog('close');
        }
    }],
    open: function() {
        $("#close-button").focus();    // <-- select the close button via the id
    }});

这适合我。