0
I have this function, but the return does not work in it, I do not want it to leave it, until it conforms to the function:
if ($('#FreteComprador').prop("checked") == true) {
var id = $("#idtransportador").val();
var frete = $("#Frete").val();
if (id == "") {
alert("Preencha o campo transportador corretamente.");
return;
} else {
var url = "/PedidoFornecedor/VerificaCNPJ";
$.ajax({
url: url
, data: { id: id }
, type: "POST"
, datatype: "html"
, success: function (data) {
if (data.resultado == true && frete == "0,00") {
alert("É obrigatório preencher o valor do frete.");
return;
}
}
});
}
}
How can I do it? In other ifs, when I put return
works, in this he leaves the function.
I believe that there is a problem in your code which is the following, you are firing the ajax before validating the freight. I don’t know about your business rule but this is a little strange, in the Success of the ajax requisition that is testing whether it was filled?
– gabrielfalieri
It is because he has to check if the freight is by the supplier himself he does not need to be filled in, so this function, if in sucess he returns true, he has to be required to fill in the freight.
– Mariana
Where is the problem in the code?
– gabrielfalieri
He leaves on Return, he should stop there and not enter the next if.
– Mariana
try to set Return false;
– gabrielfalieri
It didn’t work, it goes in the next if, and then back in this.
– Mariana
Let’s go continue this discussion in chat.
– gabrielfalieri
The function
$.ajax
by default works in a way async, that is, it will not return the result in the next line of code, for this you must use the callbacksuccess:function(){}
to continue your checks, remembering that there is also a callback for the case of error in the request. jQuery Ajax– Icaro Martins