Submit sending multiple times

1

I have a form where I check if the checkboxes are selected before submitting the Submit, however when the return is false (no checkbox is selected), after selecting the checkbox and clicking the submit button, the Form is sent more than 1 turn. When the validation is returned True the first time, the submit is sent only once, ie I have no problem.

NOTE: I can not change the submit button to button because I need the validation of the mandatory fields of the form that the submit does. The .click () also needs to identify the button that was clicked.

Could anyone help unravel this mystery? Or to do otherwise with the same result.

This is jquery.

        function validaFormaPagto(){
            var tipopag = $(".tipopag").find(":checked").length;
            if (tipopag == 0) {
                return false;
            }
        }

        //Evento quando clicar em botões da classe concluir
        $(".concluir").click(function(){
            var valorBtn    = $(this).val();
                $("#forminput").submit(function(e){
                    var data = $("#forminput").serialize();
                    var numped = $('#numped').val();
                    if (!validaFormaPagto() ) {
                        return false;
                    }
                    $.ajax({
                        type : 'POST',
                        url  : 'config/concluirPedido.php',
                        data : data, //botao,
                        dataType: 'json',
                        beforeSend: function()
                        {   
                            $("#concluir").attr('disabled','disabled');
                            $("#imprimir").attr('disabled','disabled');
                        },
                        success :  function(response){                      
                            if(response.codigo == '2'){ 
                                $('#concluir').removeAttr('disabled');
                                $('#imprimir').removeAttr('disabled');
                                $('#forminput').fadeOut("fast");
                                $('#limpar').fadeOut("fast");
                                $('#mensagem').css('display', 'block')
                                .removeClass()
                                .addClass(response.tipo)
                                .html('')
                                .html('<span>' + response.mensagem + '</span>');

                                //Esconde a mensagem após 6 segundos e recarrega/redireciona a página
                                $("#mensagem").delay(4000).fadeOut("fast",function(e){
                                    window.location.reload();
                                });
                                //Verifica se imprimi o Pedido
                                if (valorBtn == 'Confirmar + Imprimir') {
                                    window.open('relimpressaoPedido.php?termo='+numped);
                                }
                            }
                            else {
                                $('#mensagem').css('display', 'block')
                                .removeClass()
                                .addClass(response.tipo)
                                .html('')
                                .html('<span>' + response.mensagem + '</span>');
                                //Esconde a mensagem após 6 segundos
                                $("#mensagem").delay(6000).fadeOut("fast");
                            }
                        }
                    });
                    e.preventDefault();
                    return false;
                });                    
        });
    
asked by anonymous 09.11.2017 / 15:20

1 answer

0

I believe it is because, after the return of the false, the code outside the keys continues to run, regardless of the return. You can try to put the ajax inside the else. For example:

if (!validaFormaPagto() ) {
     return false;
} else {
     $.ajax...
}
    
23.01.2018 / 19:29