问题
I have a jquery dialog and I need to execute some_code() when I press Esc, or click the cancel button. With the cancel button its easy adding a function to the cancel button.
$( '#mydialog' ).dialog({
closeOnEscape: true,
close: function( event, ui ) {
//some_code();
$(this ).dialog( 'destroy' )
},
buttons: {
"OK" : function() {
$( this ).dialog( "close" );
},
"Cancel" : function() {
some_code();
$( this ).dialog( "close" );
}
}
});
But how can I execute some_code() after pressing ESC? This function must not be called clicking the OK button, so I cannot simply place it to the close event.
回答1:
After googling around without finding an answer (and that's why I asked the question Q&A style) I started looking into close: function( event, ui )
part and found the solution using event.originalEvent
close: function( event, ui ) {
//some_code();
if(event.originalEvent ){
// triggered by clicking on dialog box X or pressing ESC
// not triggered if a dialog button was clicked
some_code();
}
$(this ).dialog( 'destroy' )
}
hope this helps somebody, here's the fiddle: http://jsfiddle.net/hbrunar/MXk3a/2/
来源:https://stackoverflow.com/questions/21219067/how-to-find-out-if-jquery-dialog-was-closed-on-escape-and-execute-some-code