Skip to content Skip to sidebar Skip to footer

Jquery: While Ajax Request Block Element

So i have this function in JS, sending a request to insert a new Status message to the database. function DoStatusInsert(){ var wrapperId = '#statusResponseNow'; $.ajax({

Solution 1:

functionDoStatusInsert(){
  $('#IdOfYourSaveButton').attr('disabled', 'disabled');
  var wrapperId = '#statusResponseNow';
    $.ajax({ 
       type: "POST",
       url: "misc/insertStatus.php",
       data: {
       value: 'y',
       uID : $('#uID').val(),
       message : $('#message').val(),
       success: function(msg){
          $('#IdOfYourSavebutton').removeAttr('disabled');
          $('#message').val("");
          $('#statusResponse').toggle(); 
          $(wrapperId).prepend(msg);
          $(wrapperId).children().first().fadeIn('slow');
        }
  });
}

enabled and disable the button. nice and easy :)

Solution 2:

On calling the function, set the disabled property of the button, and then set it back on success.

functionDoStatusInsert(){
 $('#submit').attr("disabled", "true");

 var wrapperId = '#statusResponseNow';
    $.ajax({ 
       type: "POST",
       url: "misc/insertStatus.php",
    data: {
    value: 'y',
    uID : $('#uID').val(),
    message : $('#message').val() 
    },
       success: function(msg){
         $('#message').val("");
         $('#statusResponse').toggle(); 
         $(wrapperId).prepend(msg);
         $(wrapperId).children().first().fadeIn('slow');
         $('#submit').attr("disabled", "false");
        }
     });
}

Solution 3:

My initial thoughts would be to insert

$('input[type=submit]', this).attr('disabled', 'disabled');

before the ajax call is started and then removed the disabled attribute with the success function of the ajax request.

Solution 4:

Manually toggling the disabled state of the button works well enough, but jQuery has a couple helper events to make that a bit nicer: .ajaxStart() and .ajaxStop(). You can use those two handlers on your submit button and not have to worry about maintaining that manual code around your $.ajax() request.

Just throw this in with your other initialization code, probably in $(document).ready():

$('#submit').ajaxStart(function() { this.disabled = true; });

$('#submit').ajaxStop(function() { this.disabled = false; });

Solution 5:

You can use for example jQuery BlockUI Plugin from http://jquery.malsup.com/block/ (see demo on http://jquery.malsup.com/block/#element and http://jquery.malsup.com/block/#demos).

If a div with all your form elements which you need to block has id formDiv then you can call

jQuery('#formDiv').block({ message: '<h1>Just a moment...</h1>' });

before jQuery.ajax and call

jQuery('#formDiv').unblock();

as the first line in both success and error handler of the jQuery.ajax.

Post a Comment for "Jquery: While Ajax Request Block Element"