0
I have this function below, but it’s not stopping at e.preventDefault();
, it performs and performs the submit
on the page.
Go on like I’m doing:
$('#ClienteEditar').submit(function (e) {
e.preventDefault();
var url = "/Cliente/VerificaInscricao1";
var Insc = $("#InscricaoEstadual").val();
var Tipo = $("#TipoPessoa").val();
var Id = $("#Id").val();
var Isento = $("#InscricaoIsento").prop('checked');
var form = this,
$form = $(form); // Salvamos o formulário atual em uma variável
var verificaform = 1;
$.each($('input[data-required="true"]'), function () {
if (!this.value || this.value == '') {
e.preventDefault();
verificaform = 1;
$(this).css('border', 'red 1px solid');
var id = $($(this).parents('.tab-pane')[0]).attr('id');
$('[href="#' + id + '"]').trigger('click');
return false;
}
else {
$(this).css('border', '#CCCCCC 1px solid');
}
if ($('#ClienteEditar').valid()) {
verificaform = 0;
}
});
if (verificaform == 0) {
$.ajax({
url: url,
data: { insc: Insc, id: Id, isento: Isento, tipo: Tipo },
datatype: "json",
type: "POST",
async: false,
success: function (data) {
if (data.resultado == true) {
e.preventDefault();
$("#messageI").html(data.mensagem);
if (data.mensagem != 'O campo Inscrição é obrigatório.') {
$("#InscricaoEstadual").val('');
$("#InscricaoEstadual").focus();
}
$("#InscricaoEstadual").css('border', 'red 1px solid');
var id = $($("#InscricaoEstadual").parents('.tab-pane')[0]).attr('id');
$('[href="#' + id + '"]').trigger('click');
return false;
}
}
});
var url1 = "/Cliente/VerificaDocumento1";
var Documento = $("#Documento").val();
$.ajax({
url: url1,
data: { documento: Documento, id: Id, tipo: Tipo },
datatype: "json",
type: "POST",
async: false,
success: function (data) {
if (data.resultado == true) {
e.preventDefault();
$("#message").html(data.mensagem);
$("#Documento").css('border', 'red 1px solid');
var id = $($("#Documento").parents('.tab-pane')[0]).attr('id');
$('[href="#' + id + '"]').trigger('click');
return false;
}
else {
if ($('#ClienteEditar').valid()) {
$form.off('submit').submit();
}
}
}
});
}
});
How can I fix it, I need him to perform Submit only if he enters the else
.
Try to use the function
e.stopImmediatePropagation();
– sant0will
@sant0will has not solved, occurs the same problem.
– Mariana
Ever tried to put a
return false;
at the end of the function?– fernandosavio
In the two parts I need you to stop, besides the
e.preventDefault();
, has thereturn false;
as you can see.– Mariana
You put
return false;
inside ajax callbacks and inside the.each
, the function that is the Handler of the Submit event does not returnfalse
never.– fernandosavio
@fernandosavio however I need it to perform each function at once, and depending on the return of the ajax, it stops the function, or continue. And inside the
.each
thereturn false;
works.– Mariana
Okay, to cancel the submission Handler needs to return
false
and its function does not return anything, so does not cancel the submission. As you are using synchronous AJAX you could have a variable saving the return, that would befalse
if the event should be canceled. But the point is, its function returns nothing, so cancellation is not performed.– fernandosavio
Correction: I cannot be sure if that is why the cancellation is not performed. But I believe so
– fernandosavio
Here
$form.off('submit').submit();
vc is canceling the Submit event and submitting the form without the event at the same time. Just leave$form.off('submit');
, but the Submit button will still work without the event. You have to see what you want to do when entering this if.– Sam
@fernandosavio did this,put the variable and did an if, with
return false;
and solved the problem.– Mariana