Prevent double submission of forms in jQuery

后端 未结 22 1369
旧时难觅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:19

    Why not just this -- this will submit the form but also disable the submitting button,

       $('#myForm').on('submit', function(e) {
           var clickedSubmit = $(this).find('input[type=submit]:focus');
           $(clickedSubmit).prop('disabled', true);
       });
    

    Also, if you're using jQuery Validate, you can put these two lines under if ($('#myForm').valid()).

提交回复
热议问题