如何捕获jquery对话框的返回值

时间:2014-08-26 10:22:17

标签: javascript jquery asp.net

我知道有很多像这样的问题,但我找不到合适的答案。

我的asp.net页面上有一个按钮:

<asp:LinkButton ID="lbReset" runat="server" CssClass="lbReset" OnClientClick="return ShowDialog();" ToolTip="Restart your session as if you would just have logged in">Restart</asp:LinkButton>

我有对话框内容的这个div:

<div id="dialog" title="Restart" style="display: none;">
        <p>This will clear all data of the current session (as if you would have just logged in)!</p>
        <p>Are you sure?</p>
    </div>

我有这个javascript部分的脚本:

<script type="text/javascript">

        $("div#dialog").dialog({
            modal: true,
            closeOnEscape: false,
            autoOpen: false,
            buttons:
                {
                    "Yes": function () { $("div#dialog").dialog("close"); callback(false); }
                    , "No": function () { $("div#dialog").dialog("close"); callback(true); }
                }
        }).prev().find(".ui-dialog-titlebar-close").hide();

        function ShowDialog() {
            return $("div#dialog").dialog("open");
        }

        function callback(value) {
            return value
        }

    </script>

如果我使用一个简单的确认框,我可以在用户点击“否”时停止执行代码隐藏。 我希望此对话框具有相同的行为。 但是点击哪个按钮并不重要,是或否,无论如何都会执行代码隐藏。

我在这里做错了什么?

RG。 埃里克

1 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

<script type="text/javascript">

    $(function () {
        $("#dialog").dialog({
            modal: true,
            closeOnEscape: false,
            autoOpen: false,
            buttons: 
                {
                    Yes: function () {
                       $(this).dialog("close");
                       $(this).data("callback")(true);
                },
                    No: function () {
                       $(this).dialog("close");
                       $(this).data("callback")(false);
                }
            }
       }).prev().find(".ui-dialog-titlebar-close").hide()
    });

    function ShowDialog(message, callback) {
        $('#dialog').text(message);
        $('#dialog').data("callback", callback).dialog("open");
    };

</script>