S
S
Sanzhar Bazarbek2021-04-22 21:51:20
AJAX
Sanzhar Bazarbek, 2021-04-22 21:51:20

How to prevent form resubmit via enter?

Good afternoon! I have an Ajax form on my form - after a successful submission, a modal window "Thank you" is displayed.
At this point, you can press "enter" and the form will be submitted twice. How to prevent it?

$(function() {
    $('#callBack').submit(function(e) {
        var $form = $(this);
        
        $.ajax({
            type: $form.attr('method'),
            url: $form.attr('action'),
            data: $form.serialize()
        })
        
        .done(function() {
            toggleCall();
            toggleCallModalThx();
        })
        
        .fail(function() {
            toggleCall();
            console.log('Fail');
        });
        
        e.preventDefault(); 
    });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-04-22
@Rezvor

Create a variable to record the status - sent / not sent. Before submitting the form using a banal if, check the state of this variable and submit the form only if it has not been submitted before.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question