Disable submit button on form submit

Lets consider this classic problem on the web.

Most people are not aware about the difference between double click and single click on the internet. This creates a lot of programming issues for the back-end guy.

<form method=POST>
<input type="text" id="text-input">
<input type="submit" id="submit-button">
</form>

Here is a small jquery snippet which disables the submit button on form submission. The id of the submit button would be submit-button.

$('#submit-button').click(
function(){
$('#submit-button').attr('disabled', true);
$('#submit-button').closest("form").submit();
});

This code solves most of the common problems.

Lets consider the case where there is a jquery form validation also in place.

Here you will need to disable the button only on completely validated form. The code for that goes below

$('#submit-button').click(
function(){
if($('#submit-button').closest("form").validate().form()){
$('#submit-button').closest("form").submit();
$('#submit-button').attr('disabled', true);
}});

Let me know if you find more interesting techniques.

Advertisement
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.