javascript – How to handle the submission of a form to be executed only once?

Question:

In a Method POST form, when I click the button twice quickly it performs the submit twice.

Solutions with javascript or with the spring framework.

Answer:

Disable the button after the first click, because if the request takes a long time, the user will have the perception that the request was actually sent, and will not be able to send another one.

$('form#id').submit( function( e ) {
  $( this ).children( 'input[type=submit]' ).attr( 'disabled', 'disabled' );
  e.preventDefault(); 
  return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="id">
  <input type="submit"/>
</form>
Scroll to Top