Problems changing action with Avascript

Asked

Viewed 46 times

1

I am having the following problem, I am trying to change the action of the form using the following function, what happens is that if the condition is false for any of them the same still tries to execute all others trying to execute all the conditions.

function testando(resultado) {
    var nomesconsu = null;
    var nomecampo = document.getElementById("conMarPac").value;
    $.post("/comparar", function(nomescon) {
        for (i = 0; i < nomescon.length; i++) {
            if (nomecampo == nomescon[i].nome) {
                nomesconsu = nomescon[i].nome;
            }
        }
    });
    if (nomecampo !== nomesconsu && resultado === 'marcar') {
        $('#marCon').attr('action', '/profile');
        console.log(nomesconsu);
        console.log(nomecampo);
        console.log('entrou no profile');
        return;
    } else if (nomecampo === nomesconsu && resultado === 'marcar') {
        $('#marCon').attr('action', '/incon');
        console.log('entrou no incon');
        return;
    } else if (resultado === 'salvar') {
        $('#marCon').attr('action', '/salvar');

        alert("salvo");
    }
}

1 answer

0

The variable nomesconsul is set with value within an asynchronous code, its conditions are being verified before the end of the request (POST). Try to place the checks inside the callback of the POST request ($. post).

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.