views:

150

answers:

2

.NET's Ajax helper has the LoadingElementId: String property, gets and sets the ID of the DOM element to be displayed for the time it takes to complete the request.

Looking for the easiest way to implement an 'In Process' spinner for forms submitted via jQuery's Ajax toolkit.

mny thx

A: 

jQuery has many ajaxEvents. You can tie the visibility of an element to these.

$.ajaxStart(function(){ showLoading(); });
$.ajaxComplete(function(){ hideLoading(); });
Jonathan Sampson
yes this is possible, and an easier solution than adding it in the beforeSubmit, though this logic would only work when you have a general spinner, or with giving an ID as param, you can specify where the spinner has to show...if you have lots of different loading events i suggest to do it the other way, adding it in the beforeSubmit and on complete
Sander
+5  A: 

Use the beforeSubmit & complete events of .ajax.

If you wanted to do this for all ajax calls you could use the global ajaxStart and ajaxComplete events rather than declare this everytime.

$.ajax({
  url: "test.html",
  beforeSubmit : showSpinner,
  complete : hideSpinner,
  cache: false,
  success: function(html){
    $("#results").append(html);
  }
});

function showSpinner(){

  $('#someElement').show();

}

function hideSpinner(){

  $('#someElement').hide();

}


//Using global ajax events

$.ajaxStart( showSpinner );
$.ajaxComplete( hideSpinner );
redsquare