How to find out if jquery dialog was closed on escape and execute some code

旧巷老猫 提交于 2019-12-04 03:44:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!