Prevent double submission of forms in jQuery

后端 未结 22 1711
旧时难觅i
旧时难觅i 2020-11-22 08:10

I have a form that takes a little while for the server to process. I need to ensure that the user waits and does not attempt to resubmit the form by clicking the button agai

22条回答
  •  暖寄归人
    2020-11-22 08:17

    Timing approach is wrong - how do you know how long the action will take on client's browser?

    How to do it

    $('form').submit(function(){
      $(this).find(':submit').attr('disabled','disabled');
    });
    

    When form is submitted it will disable all submit buttons inside.

    Remember, in Firefox when you disable a button this state will be remembered when you go back in history. To prevent that you have to enable buttons on page load, for example.

提交回复
热议问题