Thursday, 11 July 2013

Use jquery to prevent double submit

Recently, I was engaged in a web development project. We have encounted the very famous old problem of double submit in web application. Actually, we have many ways to solve it. We can prevent the double submit on server side. However, we do it in client side this time.

Here, I summarized two method of of preventing double submit in client side javascript.

(1) For html button, we can use jquery to disabled the button. Example code is shown here:

<input type="button" value="Click"id="subBtn"/>  
<script type="text/javascript">  

   // Disabled the button
   //execute your function e.g. submit a form

function myFunc(){  
    // ...
    // code
    // ...
    // execute your logic here
    // remove the diabled  attribute and re-enabled the button again

(2) For html anchor, we can use redirect the event handler of the anchor to another function after the first click.

<a href="javascript:;" onclick="return funcOne();">Click</a>  
function funcOne(){  
    alert("Clickthe button!");  

   //After the first click, funcTwo will be assined to funcOne.
    return false;  
function funcTwo(){  
 //function two will not do anything
    return false;  


Post a Comment