我正在使用这个插件,我将一个确认方法包装到函数中,这样我每次都可以使用它https://jsfiddle.net/8g0j4unj/
function askQuestion($msg){
$.confirm({
icon : 'fa fa-question-circle-o',
content : ''+$msg+'',
theme : 'supervan',
closeIcon: true,
animation: 'scale',
type: 'orange',
draggable:'true',
buttons:{
'Continue' : {
keys : ['enter'],
action : function(){
return 1;
}
},
'Cancel': {
keys : ['esc'],
action: function(){
this.close();
}
}
}
});
}
当我尝试此函数时,它不执行任何操作,而是继续该过程,当用户确认或取消条件时,如何将函数返回布尔值?
$(document).on('click','.sample',function(){
if(askQuestion('Are you sure want to continue without adding a server') == 1){
alert('Confirmed');
}
else{
alert('Cancelled');
}
});
我真的不知道你为什么要这样做,而这个插件有
callback
功能,你可以轻松使用它。您尝试检测用户是否单击 Confirm
或 Cancel
按钮,因此您不需要使用此:
$(document).on('click','.sample',function(){});
您的代码完全错误,因为您想通过单击按钮从函数中获取返回的数据!但有了这个?
askQuestion('Are you sure want to continue without adding a server')
实际上它什么也没返回。反正。您可以简单地使用
callback
进行检测
function askQuestion($msg){
$.confirm({
icon : 'fa fa-question-circle-o',
content : ''+$msg+'',
theme : 'supervan',
closeIcon: true,
animation: 'scale',
type: 'orange',
draggable:'true',
buttons:{
'Continue' : {
keys : ['enter'],
action : function(){
alert('Confirmed'); // if clicked on confirm
}
},
'Cancel': {
keys : ['esc'],
action: function(){
this.close();
alert('Canceled'); // if clicked on cancel
}
}
}
});
}
这可以通过
callback
功能来完成。
$(document).on('click','.sample',function(){
askQuestion('Are you sure want to continue without adding a server', function(val){
if(val==1){
alert("Msg: confirmed");
//do something
}
else{
alert("Msg : rejected");
}
});
});
function askQuestion($msg, callback){
$.confirm({
icon : 'fa fa-question-circle-o',
content : ''+$msg+'',
theme : 'modern',
closeIcon: true,
animation: 'scale',
type: 'orange',
draggable:'true',
buttons:{
'Continue' : {
keys : ['enter'],
action : function(){
callback(1);
}
},
'Cancel': {
keys : ['esc'],
action: function(){
this.close();
callback(0);
}
}
}
});
}
这就是
callback
的用法,
因此,不要在
askQuestion()
语句中使用 if()
,
在 callback
函数体内编写代码和条件。
我需要你的帮助,因为我无法回调函数中的值
看我的例子:
var data = AskQuestion('您确定要继续而不添加服务器吗', function(val){
返回值;
});
console.log(数据);
函数askQuestion(消息,回调){ $.确认({
title: '',
content: msg,
type: 'dark',
theme: 'light',
animation: 'none',
animateFromElement: false,
columnClass: 'medium',
onContentReady: function () {
},
buttons: {
ok: {
text: 'Συνέχεια',
btnClass: 'btn-dark float-right',
action: function(){
callback(56);
}//end action
},//end ok
close: {
text: 'Ακύρωση',
btnClass: 'btn-dark float-left',
action: function(){
callback(59);
}//end action
}
}
});
}
我如何将 val 返回到“data”变量并在 console.log 中打印?
console.log 现在不工作回调;
我想检查变量 56 或 59,并且仅当我有结果 56 或 57 时才能继续我的代码。
请帮忙