传递函数作为函数的回调函数

时间:2011-07-20 03:36:01

标签: javascript jquery jquery-ui jquery-plugins jdialog

我有这段代码:

$('.b-icon.b-icon_del').click(function(e) {
showConfirmDialog('Are you sure you want to delete this document?',deleteDocument(window.lastSelectedRowId), 'Delete document');                    
}); 

function showConfirmDialog(content, callback, pTitle, obj){
    return showDialogEx(content, callback, pTitle, obj, "Yes", "No");
}

function showDialogEx(content, callback, pTitle, obj, okButtonLabel, cancelButtonLabel){
    var str = "#_showMessageDialog";
    var showMessageDialog = $(str);
    if(showMessageDialog.length == 0){
        $('body').append('<div id="_showMessageDialog"></div>');
        showMessageDialog = $(str);
    }
    showMessageDialog.val("");
    showMessageDialog.append('<p id="_showMessageDialogContent">'.concat(content, '</p>'));

    var my_buttons = {};
    my_buttons[cancelButtonLabel] = function(){
        $(this).dialog("close");
        $(this).html("");
        $(this).dialog("destroy");
    };
    my_buttons[okButtonLabel] = function(){
        callback();
        $(this).html("");
        $(this).dialog("close");
        if(obj){
            obj.focus();
        }
        $(this).dialog("destroy");
    };

    showMessageDialog.dialog({
        modal : true,
        resizable : true,
        title : pTitle,
        minWidth : 250,
        width : 450,
        buttons : my_buttons
    });

}

然后我点击了包含.b-icon.b-icon_del类的按钮,此时似乎同时执行了deleteDocument(window.lastSelectedRowId)showConfirmDialog('Are you sure you want to delete this document?',deleteDocument(window.lastSelectedRowId), 'Delete document'); 。我只想在用户单击“确定”按钮后调用该回调函数(deleteDocument(window.lastSelectedRowId))。谢谢!

3 个答案:

答案 0 :(得分:2)

当您将函数deleteDocument(window.lastSelectedRowId)作为第二个参数showConfirmDialog传递时,您正在执行该函数。

您应该将它以及参数window.lastSelectedRowId传递给showConfirmDialog而不执行它。

$('.b-icon.b-icon_del').click(function(e) {
showConfirmDialog('Are you sure you want to delete this document?',deleteDocument, window.lastSelectedRowId, 'Delete document');                    
}); 

function showConfirmDialog(content, callback, rowId, pTitle, obj){
    return showDialogEx(content, callback, rowId, pTitle, obj, "Yes", "No");
}

function showDialogEx(content, callback, rowId, pTitle, obj, okButtonLabel, cancelButtonLabel){
    var str = "#_showMessageDialog";
    var showMessageDialog = $(str);
    if(showMessageDialog.length == 0){
        $('body').append('<div id="_showMessageDialog"></div>');
        showMessageDialog = $(str);
    }
    showMessageDialog.val("");
    showMessageDialog.append('<p id="_showMessageDialogContent">'.concat(content, '</p>'));

    var my_buttons = {};
    my_buttons[cancelButtonLabel] = function(){
        $(this).dialog("close");
        $(this).html("");
        $(this).dialog("destroy");
    };
    my_buttons[okButtonLabel] = function(){
        callback(rowId);
        $(this).html("");
        $(this).dialog("close");
        if(obj){
            obj.focus();
        }
        $(this).dialog("destroy");
    };

    showMessageDialog.dialog({
        modal : true,
        resizable : true,
        title : pTitle,
        minWidth : 250,
        width : 450,
        buttons : my_buttons
    });

}

答案 1 :(得分:2)

$('.b-icon.b-icon_del').click(function(e) {
showConfirmDialog('Are you sure you want to delete this document?',deleteDocument, [window.lastSelectedRowId], 'Delete document');                    
}); 

function showConfirmDialog(content, callback, callbackArguments, pTitle, obj){
    return showDialogEx(content, callbackArguments,  pTitle, obj, "Yes", "No");
}

并在:

function showDialogEx(content, callback, callbackArguments, pTitle, obj, okButtonLabel, cancelButtonLabel)

行:

my_buttons[okButtonLabel] = function(){
    callback();

应该是:

my_buttons[okButtonLabel] = function(){
    callback.apply(this, callbackArguments);

重要的是要注意callbackArguments是一个列表["value'],即使它只有一个值。

答案 2 :(得分:1)

或者,在函数中包裹deleteDocument(window.lastSelectedRowId)以防止在调用$('.b-icon.b-icon_del').click()时过早地对其进行评估。

即。

$('.b-icon.b-icon_del').click(function(e) {
showConfirmDialog('Are you sure you want to delete this document?', function() { return deleteDocument(window.lastSelectedRowId); }                  
});