在jquery中动态设置按钮文本

时间:2013-08-07 10:01:10

标签: php javascript jquery html ajax

我正在尝试在jquery中设置Dialog按钮的文本。我有2个变量,其值将动态变化。这些值应设置为Button文本。我写了以下代码。

 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'); }
    }
] });

});

但表格对话框未加载。请帮助我提出宝贵的建议。

2 个答案:

答案 0 :(得分:1)

您的代码运行正常,请确保添加了jqueryjquery ui引用,并在selector标记中添加class html,例如

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

<强> jsFiddle

更新*

请在您的js和css参考

中添加http://

一页示例

<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>

答案 1 :(得分:1)

你的div有id="selector",但在你的jquery中你打电话给$('.selector')这是为了上课。所以要么改为:

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

或者更改您的jquery代码:

$( "#selector" ).dialog();