开发者

Is there a way to check if a postback is in progress?

In the case that a button is clicked multiple times on a page - Is there a way to figure out using javascript/jquery that a postback is already in progress开发者_如何学运维 and cancels the new attempt to submit the page?

Thanks


You can avoid users from double clicking by disabling whatever form elements can cause a form submit.

Checkout http://greatwebguy.com/programming/dom/prevent-double-submit-with-jquery/ for an example.


You can disable the button on first click, so that you could not click it when the post is in progress, and re enable it when the post-back has finished.

 <script type="text/javascript" language="JavaScript">
   var submitted = false;  
   function SubmitTheForm() {
     if(submitted == true) { return; }
     document.myform.submit();
     document.myform.mybutton.value = 'Thank You!';
     document.myform.mybutton.disabled = true;
      submitted = true;
    }
 </script>
<form method="post" action="#">
<input type="submit" onclick=return SubmitTheForm()>
</form>


you could always just disable the button in the onclick handler.

$('input[type="submit"]').click(function(e) {
   e.preventDefault();
   var self = this;
   $(self).attr('disabled', 'disabled');
   $.post('url',$(self).closest('form').serialize(), function() {
      $(self).removeAttr('disabled'); // re-enable after request complete. 
   });
});


You could have your click event set a variable in your click handler to true and only allow the handler to proceed when the value is false. Of course you will have to set it to false again when your callback finishes.

if (!processInProgress) {
  processInProgress = 1
  // start the process
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜