1

我尝试在 javascript 中执行两个函数,但第二个函数必须等待第一个函数完成。

看法:

<button id="DefinirEstab" class="btn btn-success btn-lg" data-toggle="modal" data-target="#myModal" onclick="saveContrato(); CountEstab()"> Define
 </button>

第一个函数js:

function saveContrato() {
$("#formContrato").ajaxSubmit({
    type: "POST",
    url: $("#formContrato").attr("action"),
    clearForm: false,
    success: function () {
        $("#Serie").attr("disabled", true)
    }
});
$("#DefinirEstab").attr("onclick", "CountEstab()");

}

第二个函数js(等待第一个完成):

function CountEstab() {

    //Guardar os dados na tabela serviço
    var Serie = $("#Serie").val();
    var NumDoc = $("#NumDoc").val();
    $.getJSON("/Contrato/saveCabecalhoServicos", {
        serie: Serie, numDoc: NumDoc, idForn: window.IdFornecedor, idFilial: e
    });

} else {
    alert("Falta de dados obrigatórios preenchidos!!");
}

}

4

2 回答 2

1

CountEstab()在里面打电话success function

html

<button id="DefinirEstab" class="btn btn-success btn-lg" data-toggle="modal" data-target="#myModal"> Define
</button>

javascript

$('#DefinirEstab').on('click', function() { 
    saveContrato();
    return false;
});   

function saveContrato() {
    $("#formContrato").ajaxSubmit({
        type: "POST",
        url: $("#formContrato").attr("action"),
        clearForm: false,
        success: function () {
            $("#Serie").attr("disabled", true);
            CountEstab();
        }
    });
}
于 2013-11-07T17:52:31.073 回答
1

将第二个函数作为回调传递给第一个函数。

function saveContrato(callback) {
    $("#formContrato").ajaxSubmit({
        type: "POST",
        url: $("#formContrato").attr("action"),
        clearForm: false,
        success: function () {
            $("#Serie").attr("disabled", true)
            callback();
        }
    });
    $("#DefinirEstab").attr("onclick", "CountEstab()");
}

saveContrato(CountEstab)
于 2013-11-07T17:52:59.870 回答