2
Good afternoon, I have a form and I want that when the button submit
be clicked it does not reload the page for the post submission put the return false;
in function submit
of jQuery
but it did not happen...
$("#form-anuncio-edita-texto").submit(function () {
if ($("#edita-anuncio-texto").val() !== "") {
var formData = new FormData(this);
$("#page-load").toggleClass('sk-loading');
$("#anuncio-edita-text-alert").hide();
$('#modal-anuncios-3').modal('toggle');
var request = $.ajax({
url: "modulos/paginas/" + identificador + "/process/updateMedia.php",
method: "POST",
data: formData
});
request.done(function (data) {
$("#page-load").toggleClass('sk-loading');
alert(data);
});
request.fail(function () {
$("#page-load").toggleClass('sk-loading');
toastNotification("<a href='index.php'>A página não pode ser carregada, se este erro persistir sujerimos que atualize a página ou clique <strong>AQUI</strong> e tente novamente.</a>", "Erro no Carregamento...", 15000, "error");
});
} else {
$("#anuncio-edita-text-alert").show();
}
return false;
});
When the field is empty he enters the else
and the page does not reload, but when I enter any value it reloads.
WARNING: This example is with a text field that would be easily solved using the function click
of jQuery
storing the contents in a var
and putting her in data: {texto: vartexto}
, but I can’t use it because in this form there is a field FILE
where I will send a file via AJAX.
try to avoid the event like this: $("form"). Submit(Function(e){ e.preventDefault(); });
– eduardo costa
@eduardocosta didn’t work, he keeps reloading the page.
– Guilherme Biancardi
And how do you have your button in the form? if it has type="Submit" try to change to type="button"
– eduardo costa
@eduardocosta if I switch to the button Submit is not sent not entering the condition above, as said in the question would be easy to solve the problem with the click event, but the problem is that I have a file field in html, I can’t get information from that field by jQuery other than via Ubmit.
– Guilherme Biancardi
Try to give a false Return within the .submit. $method call ("#form-announce-edit-text"). Submit(Function(){ Return false; });
– Pedro Enrique Univale