如何在确认框返回值上调用另一个函数。
以下是我正在使用的代码,这里只是其他部分。当我点击确认框“是”'按钮它没有调用函数abc。 TIA。
jQuery: -
var result = confirm('Are you sure');
if (result) {
abc(); //another jquery function call
}
else {
}
我也试过在浏览器控制台上写,但它只写了假。
var result = confirm('Are you sure');
if (result) {
console.log('true');
} else {
console.log('false');
}

我正在使用甜蜜警报插件,可能会遇到问题。我的确认功能如下。
function confirm(title) {
var returnType = false;
swal({
title: "Warning",//"Are you sure?",
text: title,
//text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes",
cancelButtonText: "No",
closeOnConfirm: true,
closeOnCancel: true
},
function (isConfirm, id) {
if (isConfirm) {
//window.location.href = href;
returnType = true;
//swal("Deleted!", "Record has been deleted.", "success");
} else {
//$('div').click();
//$('#' + clientid).blur();
returnType = false;
//swal("Cancelled", "Your imaginary file is safe :)", "error");
}
}
);
return returnType;
}
答案 0 :(得分:2)
在这种情况下,您可以将[function]回调设置为第二个参数。如果您想为[function]回调
传递某个[参数],这将非常有用// fnCallback = 'function callback you have set'
function confirm(title, fnCallBack) {
var returnType = false;
swal({
title: "Warning",//"Are you sure?",
text: title,
//text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes",
cancelButtonText: "No",
closeOnConfirm: true,
closeOnCancel: true
},
function (isConfirm, id) {
if (isConfirm) {
// Call [fnCallback] method with your [returnType] value (e.g true)
returnType = true;
fnCallBack(returnType);
} else {
// Call [fnCallback] method with your [returnType] value (e.g false)
returnType = false;
fnCallBack(returnType);
}
}
);
return returnType;
}
然后当你调用[confirm]函数/方法时,你需要分配两个参数(标题和函数本身)。现在,我们将[匿名]使用给定的returnType
参数进行分配。
$('.dom-element').click(function() {
confirm('Invalid data found', function(returnType) {
// If the returnType given from the [confirm] method
// is false, call another [swal] method
if (returnType !== true) {
setTimeout(function() {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}, 200);
return false;
}
// If its true, then:
setTimeout(function() {
swal("Deleted!", "Record has been deleted.", "success");
}, 200);
return true;
});
});
如果您需要在流程中传递一些值,这可能对您有所帮助。希望这会有所帮助。
PS:如果你只返回[sval]插件的警告信息,你可以在[confirm]函数中调用[setTimeout] async函数,而不是使用[function]回调,如上图所示。