How to determine what caused the close event in the jquery ui dialog

I have Create and Cancel buttons as part of the jquery-ui modal dialog. I want to do something after closing the dialog only if the user clicks "Create". If they press "Cancel" or "X" or press "Esc", I want to do something else. Is there a way to pass parameters to the close event handler, or in some other way determine what caused the close?

+3
source share
4 answers

try this as described here: http://jqueryui.com/demos/dialog/#modal-confirmation

- edit

copied from the link above:

    var trigger = "";
    $("#dialog").dialog({
        bgiframe: true,
        resizable: false,
        height:140,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            'Create': function() {
                // do your stuff
                trigger = "create";
                $(this).dialog('close');
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        }
        close: function() {
            if (trigger == "create")
                // do something here
        }
    });
+4
source

, , ( 1).

, [x], -.

, , Escape, -.

, .

, - event.which. [x] , ( 1 3), Escape , key code (27). event.which undefined.

...

, Create - , .

- [x], Escape . cancel() ( - ), Cancel , event.which .

- ...

buttons: {
    'Create': function() {
        // do your stuff here
        $(this).dialog('close');
    },
    Cancel: function() {
        cancelled();
        $(this).dialog('close');
    }
},
close: function(event) {
    if (event.which) {
        cancelled();
    }
}

, , , , , .

+5

:

$("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        height: 300,
        modal: true,
        buttons: {
            'Create': function() {
                alert('the user clicked create');
                $(this).dialog('close');
            },
            Cancel: function() {
                alert('the user clicked cancel');
                $(this).dialog('close');
            }
        },
        close: function() {
           // do somthing

        }
});

, close, :

function(event, ui)

+4
$('#jqPopup').dialog({modal:true,title:tit,autoOpen:false,beforeclose:function(){
   //Will get triggered when user press the 'X' button 
   $("#wholeContent").css('display','block');} 
});

$("#wholeContent").css('display','none');

$('#jqPopup').dialog('open');
0

Source: https://habr.com/ru/post/1716003/


All Articles