Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.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对话框_Javascript_Jquery_Modal Dialog - Fatal编程技术网

Javascript 跨项目自定义确认jquery对话框

Javascript 跨项目自定义确认jquery对话框,javascript,jquery,modal-dialog,Javascript,Jquery,Modal Dialog,我必须创建jquery的确认模式框,它作为默认的确认框,返回true或false,并在整个项目中工作 我这样称呼它 var result = confirm("Are you sure you want exit"); if(result == false) return false; else{ //somethings are done here } 在Main.js中,它的实现如下所示 function confirm(message){ $("#alert_c

我必须创建jquery的确认模式框,它作为默认的确认框,返回true或false,并在整个项目中工作

我这样称呼它

var result = confirm("Are you sure you want exit");
if(result == false)
     return false;
else{
    //somethings are done here
}
在Main.js中,它的实现如下所示

function confirm(message){
    $("#alert_cust_message").html(message);
    var returnValue = false;
    $("#myAlert").dialog({
        modal: true,
        minHeight: 100,
        buttons: {
            "OK": function() 
            {
                $( this ).dialog( "close" );
                returnValue = true;
            },
            Cancel: function() 
            {
                $( this ).dialog( "close" );
                returnValue = false;
            }
        }
    });
    $("#myAlert").parent().css('font-size','9pt');
    return returnValue;
}
现在我面临的问题是关于返回值

我没有得到预期的回报,我认为这是由于异步对话

现在这个问题能很容易解决吗


任何帮助都将不胜感激。。谢谢

您需要使用回拨

function confirm(message, callback) {
    $("#alert_cust_message").html(message);
    var returnValue = false;
    $("#myAlert").dialog({
        modal: true,
        minHeight: 100,
        buttons: {
            "OK": function () {
                $(this).dialog("close");
                callback(true);
            },
            Cancel: function () {
                $(this).dialog("close");
                callback(false);
            }
        }
    });

    $("#myAlert").parent().css('font-size', '9pt');
}
然后


演示:

我可以避免回调部分吗?我的意思是有没有一种不用回调函数的方法?@Saurabh不太可能。。。另一个解决方案是使用deferred/promise感谢您的解决方案。。。。承诺是一份有前途的工作。。因此,我们将采用此解决方案,再次感谢。
confirm('some message', function (result) {
    if (result) {}
})