我有这个代码:
$('.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/