Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
After ajax submit, the message is repeated

When I run submit, an alert message occurs as many times as you send it before you reload.

For example.

first run submit occur success message

not refresh second run submit two repeat occurs success message

not refresh third run submit three repeat occurs success message

...

not refresh n run submit n repeat occurs success message

Why does it run like this? How can I solve this problem?

My code is as follows.

$(document).ready(function() {
            $('#my_modal').on('show.bs.modal', function(e) {
                var p_index = $(e.relatedTarget).data('p_index');
                $(e.currentTarget).find('input[name="p_index"]').val(p_index);

                $("button#submit").click(function() {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: "../receipt/send.php",
                        data: $('form.send_p_index').serialize(),
                        success: function(data) {
                            alert("success")
                            $("#send_p_index")[0].reset()
                            $("#my_modal").modal('hide');
                        },
                        error: function() {
                            alert("Error");
                        }
                    });
                });

            });
        }
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

WRONG

$(document).ready(function() {
            $('#my_modal').on('show.bs.modal', function(e) {
                var p_index = $(e.relatedTarget).data('p_index');
                $(e.currentTarget).find('input[name="p_index"]').val(p_index);

                $("button#submit").click(function() {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: "../receipt/send.php",
                        data: $('form.send_p_index').serialize(),
                        success: function(data) {
                            alert("success")
                            $("#send_p_index")[0].reset()
                            $("#my_modal").modal('hide');
                        },
                        error: function() {
                            alert("Error");
                        }
                    });
                });

            });
        }

CORRECT: Put this outside of modal.shown because every time the modal shown, it will write a submit function that repeats as many as it shown.

 $(document).on('click', 'button#submit', function() {
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: "../receipt/send.php",
                        data: $('form.send_p_index').serialize(),
                        success: function(data) {
                            alert("success")
                            $("#send_p_index")[0].reset()
                            $("#my_modal").modal('hide');
                        },
                        error: function() {
                            alert("Error");
                        }
                    });
});
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda