How can I prevent a double submit with jQuery or Javascript?

前端 未结 10 1270
终归单人心
终归单人心 2020-12-08 22:44

I keep getting duplicate entries in my database because of impatient users clicking the submit button multiple times.

I googled and googled and found a few scripts,

10条回答
  •  离开以前
    2020-12-08 22:53

    The problem with the method described here is that if you're using a javascript validation framework and the validation fails, you won't be able to correct and re-submit the form without refreshing the page.

    To solve this, you need to plug into the success event of your validation framework and only then, set the submit control to disabled. With Parsley, you can plug into the form validated event with the following code:

    $.listen('parsley:form:validated', function(e){
        if (e.validationResult) {
            /* Validation has passed, prevent double form submissions */
            $('button[type=submit]').attr('disabled', 'disabled');
      }
    });
    

提交回复
热议问题