javascript - 将函数作为回调传递给函数

标签 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( '您确定要删除该文档吗?',deleteDocument(window.lastSelectedRowId), '删除文档'); 目前。我只想在用户单击“确定”按钮后调用回调函数(deleteDocument(window.lastSelectedRowId))。谢谢!

最佳答案

当您将函数 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
    });

}

关于javascript - 将函数作为回调传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6756733/

相关文章:

ASP.NET + jQuery UI 模态 : load aspx page into modal dialog

javascript - jQuery ui slider 错误的 ui 值

javascript - jqGrid 编辑最后一行时客户端验证模式未正确放置

javascript - 与 Angular 2 中的多个组件共享 websocket 数据

html - DOM 禁用属性

javascript - 为什么通过 javascript 发送多个打印被 firefox 阻止以及如何禁用它?

javascript - node.js 控制台 - Node 控制台中的 `undefined` 是什么?

javascript - 具有动画高度的 jQuery Accordion 式菜单

javascript - jQuery仅在最后一个子元素可见时添加css类

javascript - 动态添加选项到脚本