简体   繁体   中英

jQuery UI dialog: How check what if user confirm or cancel the UI dialog.

I have this code :

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

How can I check what user clicked, Confirm or Cancel ???

If possible I would like change Javascript part2 only.

Change the structure of your buttons code to:

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

Then you have easy access to the click event for each button.

jsFiddle example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM