Cancel sending jQuery form

Asked

Viewed 3,896 times

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(); });

  • @eduardocosta didn’t work, he keeps reloading the page.

  • And how do you have your button in the form? if it has type="Submit" try to change to type="button"

  • @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.

  • Try to give a false Return within the .submit. $method call ("#form-announce-edit-text"). Submit(Function(){ Return false; });

2 answers

4


Use the Event.preventDefault() to prevent the submit is executed, call the function by passing the event as parameter.

Example:

$("#form-anuncio-edita-texto").submit(function(event){
   event.preventDefault();
});

Event.preventDefault()

Cancel the event if cancellable, without stopping the spread of the event.

$("#form-anuncio-edita-texto").submit(function(event){
  event.preventDefault();
  console.log("Entrei mas não enviei");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form method="post" action="pagina2.asp" id="form-anuncio-edita-texto">
  <button type="submit" id="btn">
    Enviar
  </button>
</form>

  • I am not avoiding the button Ubmit since the ID (#form-announce-edit-text) is the ID of the button form not.

  • Opa, I did not have the HTML imagined that it was the button, but anyway using Event.preventDefault() passing the event as parameter you will get the expected result.

  • Friend @eduardocosta gave this suggestion, but it didn’t work.

  • In the example I put above works perfectly, I believe it may be something in your javascript that is causing unexpected behavior, came to check if there is error in the console?

  • if some error I can’t see, the page refreshes very fast before even appearing something.

  • Has 2 ways for you to see 1- Checking the option of manter log in the developer tool, or change the button type to just debuggar button and see the error.

  • the error that gives is this: Uncaught Typeerror: Illegal Invocation at e (jquery-3.2.1.min. js:4) at Ab (jquery-3.2.1.min. js:4) at Function.r.param (jquery-3.2.1.min. js:4) at Function.ajax (jquery-3.2.1.min. js:4) at Htmlformelement.<Anonymous> (<Anonymous>:146:29) at Htmlformelement.Dispatch (jquery-3.2.1.min. js:3) at Htmlformelement.q.Handle (jquery-3.2.1.min. js:3)

  • The problem is how you pass the data to your ajax, you are passing a jquery object and it is not being able to interpret it. I suggest opening a new question specifying the error.

Show 3 more comments

0

I think you have to do something like this:

<form id="form">
 <input type="text" name="name" id="name">
 <button type="button" id="submit">teste</button>
</form>

<script>
 $('#submit').click(function () {
   $('#form').submit();
 });
 $('#form').submit(function (e) {
   e.preventDefault();
   if //logica
 });
</script>

Browser other questions tagged

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