jQuery模式对话框按钮文本

Mar*_*eon 8 jquery jquery-ui modal-dialog jquery-ui-dialog

根据下面的代码显示JQuery对话框,按钮文本显示为文字"b"而不是变量b的值.

即:showWarningDialog('myBody', 'myTitle', 'go')显示一个带有标记的按钮b而不是的对话框go.

你怎么能go出现?

function showWarningDialog(theBody, theTitle, buttonText) {
    var t = "Warning";
    if (theTitle != null) {
        t = theTitle;
    }

    var b = "Ok";
    if (buttonText != null) {
        b = buttonText;
    }

    $("#div-dialog-warning div").text(theBody);

    $("#div-dialog-warning").dialog({
        title: t,
        resizable: false,
        height: 160,
        modal: true,
        buttons: {
            b : function () {
                $(this).dialog("close");
            }
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*all 13

根据jQuery UI文档,按钮名称来自buttons对象中按钮的键.在这种情况下,替换这个位:

$("#div-dialog-warning").dialog({
    title: t,
    resizable: false,
    height: 160,
    modal: true,
    buttons: {
        b : function () {
            $(this).dialog("close");
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

有了这个:

var buttonOpts = {};
buttonOpts[b] = function () {
    $(this).dialog("close");
};
$("#div-dialog-warning").dialog({
    title: t,
    resizable: false,
    height: 160,
    modal: true,
    buttons: buttonOpts
});
Run Code Online (Sandbox Code Playgroud)

你必须把它b看作一个变量,因此使用buttonOpts[b]而不是你所做的,这相当于使用buttonOpts.b.