I have this code that does validation to a form if it has fields fields if it does not have it submit form:
$("#btnAdd").click(function (e) {
e.preventDefault();
var erros = 0;
$("#formAdd input").each(function () {
$(this).val() == "" ? erros++ : "";
});
$("#formAdd textarea").each(function () {
$(this).val() == "" ? erros++ : "";
});
if (erros > 0) {
$("#msg").show().fadeIn(500).delay(1500).fadeOut(600);
} else {
$(this).submit(function () {
var form = $(this);
var dados = new FormData($(this)[0]);
$("#btnAdd").prop("disabled", true);
$.ajax({
url: 'u/add',
cache: false,
contentType: false,
processData: false,
data: dados,
type: 'post'
}).done(function (data) {
fetchUser();
alert("Dados Salvos com Sucesso");
console.log("STATUS : ", data);
$("#btnAdd").prop("disabled", false);
$('#modalAddfoto').modal('hide');
$('.addfoto')[0].reset();
}).fail(function (jqXHR, textStatus) {
console.log("Request failed: " + textStatus);
$("#btnAdd").prop("disabled", false);
});
return false;
});
}
});
It is valid normal but is not entering this part of the code:
$(this).submit(function () { ... }
Detail, if I take the validation and make $('#formAdd').submit(function () { ... }
it works normally.