javascript 单击 JQuery 中的按钮打开 UI 对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18201766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:04:07  来源:igfitidea点击:

Open an UI Dialog on click of a button in JQuery

javascriptjqueryhtmlajaxjquery-ui

提问by Sanjay Malhotra

My previous qstn in this forum was about assigning text dynamically to buttons in jquery dynamically and I got solution for that here. Now my question is, upon clicking that button, I have to open another UI dialog with 2 buttons. I have written the following code. I am able to open UI Dialog but buttons are not appearing. Pls help me to alter my code.

我之前在这个论坛上的 qstn 是关于动态地将文本动态分配给 jquery 中的按钮,我在这里得到了解决方案。现在我的问题是,单击该按钮后,我必须打开另一个带有 2 个按钮的 UI 对话框。我编写了以下代码。我可以打开 UI 对话框,但没有出现按钮。请帮我修改我的代码。

<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() {
                     $("#opener").dialog({modal: true, height: 590, width: 1005 });
                        $(this).dialog("close");
                    }
                },
                {
                    text: nextMonth,
                    click: function() {
                        $(this).dialog('close');
                    }
                }
            ]});

        </script>
        <script>

          $(".opener").dialog({buttons: [
                {
                    text: "ok",
                    click: function() {
                        $(this).dialog("open");
                    }
                },
                {
                    text: "cancel",
                    click: function() {
                        $(this).dialog('open');
                    }
                }
            ]});

    </script>

    <div id="opener" title="Pop Up" class = "opener" width ="100px"> 
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
    Score will be updated</p> </div>


</body>

回答by Dennis

Well, you have not specified any buttons to show in this line of code:

好吧,您还没有在这行代码中指定要显示的任何按钮:

$("#opener").dialog({modal: true, height: 590, width: 1005 });

Maybe you wanted to intiliazie it like this, without opening:

也许你想像这样初始化它,而不用打开:

  // did you mean to select #opener or .opener??
  $("#opener").dialog({buttons: [
        {
            text: "ok",
            click: function() {
                $(this).dialog("open");
            }
        },
        {
            text: "cancel",
            click: function() {
                $(this).dialog('open');
            }
        },
        autoOpen: false
    ]});

And then just open it in the other line from the other dialog like this:

然后在另一个对话框的另一行中打开它,如下所示:

$("#opener").dialog('open');