2

我正在尝试在 jquery 中设置对话框按钮的文本。我有 2 个变量,其值会动态变化。这些值应设置为按钮文本。我已经编写了以下代码。

 var monthNames = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ];

   var today = new Date();
   var month = monthNames[today.getMonth()];
   var nextMonth = monthNames[today.getMonth()+1];

  $( ".selector" ).dialog({ buttons: [
    {
        text: month,
        click: function() { $(this).dialog("close"); }
    },
    {
        text: nextMonth,
        click: function() { $(this).dialog('close'); }
    }
] });

});

但是表单对话框没有加载。请帮助我提出您的宝贵建议。

4

2 回答 2

1

您的代码工作正常,请确保您已添加jqueryjquery ui引用并selector class在您的html标记中包含,例如

<div class="selector"></div>

jsFiddle

更新*

请添加http://您的 js 和 css 参考

一页示例

<html>
    <head>
        <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> 
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    </head>
    <body>
        <div id="selector" title="Pop Up" class = "selector"> <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span> Do u want to save the score?</p> </div>
        <script>
            var monthNames = ["January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"];

            var today = new Date();
            var month = monthNames[today.getMonth()];
            var nextMonth = monthNames[today.getMonth() + 1];

            $(".selector").dialog({buttons: [
                    {
                        text: month,
                        click: function() {
                            $(this).dialog("close");
                        }
                    },
                    {
                        text: nextMonth,
                        click: function() {
                            $(this).dialog('close');
                        }
                    }
                ]});
        </script>
    </body>
</html>
于 2013-08-07T10:09:21.403 回答
1

你的 div 有id="selector",但在你的 jquery 中你调用$('.selector')的是类。因此,要么更改为:

<div class="selector"></div>

或更改您的 jquery 代码:

$( "#selector" ).dialog();
于 2013-08-07T11:21:35.783 回答