jQuery UI对话框:如果用户确认或取消UI对话框,如何检查



我有这样的代码:

HTML:

<div id="dialog-first"></div>
<div id="dialog-second"></div>
<div id="dialog-third"></div>
<div id="menu">
    <ul>
        <li id="first"><a href="#first">first</a></li>
        <li id="second"><a href="#second" class="confirm">second (need confirm)</a></li>
        <li id="third"><a href="#third" class="confirm">third (need confirm)</a></li>
    </ul>
</div>

Javascript part 1:

$('li a.confirm').live('click', function (e) {
    e.preventDefault();
    var id = $(this).parent('li').attr('id');
    $('#dialog-' + id).dialog({
        dialogClass: 'ui-dialog-confirm',
        buttons: {
            "Confirm": function () {
                /*window.location.href = $(this).attr('href');*/
                $(this).dialog("close");
            },
                "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });
});

Javascript part 2:

$('#menu li').each(function () {
    var id = $(this).attr('id');
    $('#dialog-' + id).on("dialogclose", function (event, ui) {
        //How can I check what user clicked, Confirm or Cancel ???
        //Is it possible
        console.log(event);
    });
});

如何查看用户点击了什么,确认还是取消?

如果可能的话,我只想改变Javascript part2。

将您的按钮代码结构更改为:

buttons: [{
    text: "Confirm",
    click: function () {
        $(this).dialog("close");
        console.log('confirm')
    }
}, {
    text: "Cancel",
    click: function () {
        $(this).dialog("close");
        console.log('cancel')
    }
}]

然后,您可以轻松访问每个按钮的单击事件。

<<p> jsFiddle例子/strong>

最新更新