【问题标题】:jQuery modal dialog button textjQuery 模态对话框按钮文本
【发布时间】:2010-10-25 15:28:11
【问题描述】:

根据下面显示 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");
            }
        }
    });
}

【问题讨论】:

    标签: jquery jquery-ui modal-dialog jquery-ui-dialog


    【解决方案1】:

    As per the jQuery UI docs,按钮名称来自buttons对象中的按钮键。在这种情况下,替换这个位:

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

    用这个:

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

    您必须将b 视为变量,因此使用buttonOpts[b] 而不是您所做的,这是使用buttonOpts.b等效

    【讨论】:

      【解决方案2】:

      这是您在初始化对话框后需要添加的内容:

       $('div.ui-dialog-buttonpane button:contains(b)').empty().html(b);
      

      您可能希望将 b 重命名为更具描述性和独特性的名称。

      【讨论】:

        【解决方案3】:

        {b: 'blah'} 意味着它将 b 作为变量名。 手动定义数组可以修复它,虽然我无法想象没有特殊的语法 变量按钮 = {}; 按钮[b] = function() { }; $().dialog({buttons: 按钮});

        【讨论】:

          【解决方案4】:

          我认为您在 conig 数组中缺少参数。应该有类似

          buttons: {
                  b : function () {
                      $(this).dialog("close");
                  },
                  buttonText: b
          }
          

          【讨论】:

          • 没有buttonText 选项。根据文档,The property key is the text of the button.
          猜你喜欢
          • 2011-06-03
          • 1970-01-01
          • 1970-01-01
          • 2012-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-10
          • 2010-11-30
          相关资源
          最近更新 更多