Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery confirm.js将结果返回到布尔值_Javascript_Jquery_Confirm - Fatal编程技术网

Javascript jQuery confirm.js将结果返回到布尔值

Javascript jQuery confirm.js将结果返回到布尔值,javascript,jquery,confirm,Javascript,Jquery,Confirm,我使用这个,我在函数中包装了一个确认方法,这样我每次都可以使用它 当我尝试使用此函数时,它不会执行任何操作,但会继续此过程。当用户确认或取消条件时,如何将函数返回到布尔值 $(document).on('click','.sample',function(){ if(askQuestion('Are you sure want to continue without adding a server') == 1){ alert('Confirmed');

我使用这个,我在函数中包装了一个确认方法,这样我每次都可以使用它

当我尝试使用此函数时,它不会执行任何操作,但会继续此过程。当用户确认或取消条件时,如何将函数返回到布尔值

$(document).on('click','.sample',function(){
    if(askQuestion('Are you sure want to continue without adding a server') == 1){
                alert('Confirmed');
            }
    else{
       alert('Cancelled');
    }

});

我真的不知道为什么你想这样做,而这个插件有
回调
函数,你可以很容易地使用它。您试图检测用户是否单击了
确认
取消
按钮,因此您不需要使用此按钮:

$(document).on('click','.sample',function(){});
您的代码完全错误,因为您希望通过单击按钮从函数中获取返回的数据!但是用这个

askQuestion('Are you sure want to continue without adding a server') 
实际上它什么也不返回。无论如何只需使用
回调

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
                }
            }
        }
    });
}

这可以通过
回调
函数来完成

$(document).on('click','sample',function(){
askQuestion('您确定要继续而不添加服务器',函数(val){
如果(val==1){
警报(“消息:已确认”);
//做点什么
}
否则{
警报(“消息:已拒绝”);
}
});
});
函数askQuestion($msg,回调){
美元。确认({
图标:“fa-fa-question-circle-o”,
内容:“+$msg+”,
主题:"现代",,
closeIcon:是的,
动画:“缩放”,
键入:“橙色”,
可拖动:'true',
按钮:{
“继续”:{
键:[“输入”],
行动:功能(){
回调(1);
}
},
“取消”:{
键:['esc'],
行动:功能(){
这个。关闭();
回调(0);
}
}
}
});
}
这就是
回调
的使用方式

因此,不要在
if()
语句中使用
askQuestion(),
在
回调
函数体中编写代码和条件


你的
askQuestion
函数不会返回任何表示
if
条件将始终为
false
。你认为“return true”或“return false”如何?@sachinkumar我曾经尝试过不起作用的方法。请尝试此方法-返回“1”而不是返回1,并与“1”而不是1进行比较。或返回“真/假”。总的来说,请尝试使用双引号。如果(askQuestion(‘您确定要继续而不添加服务器’)==1),从您的示例中,askQuestion没有返回任何值,则警报来自“action:function()”,而不是来自if()条件的内部。正如所料。
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
                }
            }
        }
    });
}