Not stopping Ubmit with e.preventDefault();

Asked

Viewed 145 times

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 has not solved, occurs the same problem.

  • Ever tried to put a return false; at the end of the function?

  • In the two parts I need you to stop, besides the e.preventDefault();, has the return false; as you can see.

  • You put return false; inside ajax callbacks and inside the .each, the function that is the Handler of the Submit event does not return false never.

  • @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 the return false; works.

  • 3

    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 be false if the event should be canceled. But the point is, its function returns nothing, so cancellation is not performed.

  • Correction: I cannot be sure if that is why the cancellation is not performed. But I believe so

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

  • @fernandosavio did this,put the variable and did an if, with return false; and solved the problem.

Show 5 more comments
No answers

Browser other questions tagged

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