从jquery对话框中获取返回值;像警报框一样异步

时间:2016-06-09 10:28:20

标签: javascript jquery jquery-plugins

function confirmation(question) {
    var defer = $.Deferred();
    $('<div></div>').html(question).dialog({
        autoOpen: true,
        modal: true,
        title: 'Confirmation',
        buttons: {
            "Yes": function() {
                defer.resolve("true"); //this text 'true' can be anything. But for this usage, it should be true or false.
                $(this).dialog("close");
            },
            "No": function() {
                defer.resolve("false"); //this text 'false' can be anything. But for this usage, it should be true or false.
                $(this).dialog("close");
            }
        },
        close: function() {
            $(this).remove();
        }
    });
    return defer.promise();
}

function ValidateForm() {
    alert("step2");
    var question = "Do you want to start a war?";
    confirmation(question).then(function(answer) {
        var ansbool = Boolean.parse(answer.toString());
        if(ansbool) {
            alert("this is obviously " + ansbool); //TRUE
            return true;
        }else{
            alert("and then there is " + ansbool); //FALSE
            return false;
        }
    });
    alert("step3"); // it gets call before dialog is open and return results on user selection Yes/No , i want to wait and return result true or false from this function
}

function calltovalidateform()
{
    alert("step1");
    var returnValue = ValidateForm(); // must return true or false value
    alert("step4" + returnValue); // returnValue is undefined.
    return returnValue;
}

以上javascript代码按步骤1,步骤2,步骤3&amp;的顺序写入结果。 step4警告消息。我想要ValidateForm()的结果,它打开一个使用jquery模型对话框的对话框,并异步返回值true或false。无需在运行时跳过其确认结果。 我的要求是等待javascript,直到用户在模型对话框中提供是或否的混淆。

0 个答案:

没有答案