jQuery对话框:跟踪是否单击“确定”或“取消”

时间:2013-09-09 13:50:15

标签: jquery

在下面的jQuery UI对话框中,如何在“关闭”回调函数中跟踪单击“确定”或“取消”?

$( "#dialog-form" ).dialog( "option", "buttons", 
    [ 
        { 
            text: "OK", 
            click: function() { 
                // sone action...
                $( this ).dialog( "close" );
            }
        },
        {
            text: "Cancel", 
            click: function() { 
                $( this ).dialog( "close" );
            }
        }
    ] 
);

$( "#dialog-form" ).dialog({
    close: function(event, ui) {
        // Track here whether 'OK' or 'Cancel' was clicked...
    }
});

1 个答案:

答案 0 :(得分:0)

除非有内置的方法,否则你可以这样做:

$( "#dialog-form" ).dialog( "option", "buttons", 
    [ 
        { 
            text: "OK", 
            click: function() { 
                // sone action...
                $("#dialog-form").data({ lastClick: 'ok' });
                $( this ).dialog( "close" );
            }
        },
        {
            text: "Cancel", 
            click: function() { 
                $("#dialog-form").data({ lastClick: 'close' });
                $( this ).dialog( "close" );
            }
        }
    ] 
);

$( "#dialog-form" ).dialog({
    close: function(event, ui) {
        // Track here whether 'OK' or 'Cancel' was clicked...
        var lastClick = $("#dialog-form").data('lastClick');
        if(lastClick)
            console.log(lastClick);
    }
});