2013-04-30 50 views
0

我有一個下拉列表(id = ddl)在asp.net更新數據異步通過Ajax。現在我只想在Ajax請求初始化時顯示加載面板。那麼最好的選擇是什麼?在asp.net AJAX的jquery

此代碼是不工作...

$("#ddl").ajaxStart(function() { ShowLoadingPanel(); }).ajaxStop(); 

回答

0

//全局函數來顯示/隱藏在Ajax請求

$(document).ajaxStart(function() { 
    ShowLoadingPanel(); 
}); 

$(document).ajaxStop(function() { 
    HideLoadingPanel(); 
}); 

或者與特定項目

$("#ddl").bind("ajaxStart", function() { 
    ShowLoadingPanel(); 
}); 

$("#ddl").bind("ajaxStop", function() { 
    HideLoadingPanel(); 
}); 
將它綁定

和當你完成了Ajax調用執行解除綁定

$("#ddl").unbind(); 
+0

我可以在Doucment.ready塊使用此功能... ..? – user1849388 2013-04-30 07:05:57

+0

是的,你可以在document.ready中調用它 – 2013-04-30 07:10:02

1

對於具體的AJAX調用:

$.ajax({..., beforeSend: function(){ /* show the loading thing */ }, 
    complete: function(){ /* hide the loader */ }}); 

一般:

jQuery.ajaxSetup({ 
    beforeSend: function() { 
    $('#loader').show(); 
    }, 
    complete: function(){ 
    $('#loader').hide(); 
    }, 
    success: function() {} 
}); 

我的個人最好成績jQuery 「Please Wait, Loading…」 animation?

// a bit modified for jQuery 1.8 and error handling (CSS and instruction at the link) 
    $(document).on(
     { 
      ajaxStart : function() 
      { 
       if (!$('div.modal').length) 
       { 
        $('body').append($('<div>', 
        { 
         'class' : 'modal' 
        })); 
       } 

       $('body').addClass("loading"); 
      }, 
      ajaxStop : function() 
      { 
       $('body').removeClass("loading"); 
      }, 
      ajaxError : function(e, x, settings, exception) 
      { 
       var message, statusErrorMap = 
       { 
        '400' : "Server understood the request but request content was invalid.", 
        '401' : "Unauthorised access.", 
        '403' : "Forbidden resouce can't be accessed", 
        '500' : "Internal Server Error.", 
        '503' : "Service Unavailable." 
       }; 

       if (x.status) 
       { 
        message = statusErrorMap[x.status]; 
        if (!message) 
        { 
         message = "Unknow Error."; 
        } 
       } else if (e == 'parsererror') 
       { 
        message = "Error.\nParsing JSON Request failed."; 
       } else if (e == 'timeout') 
       { 
        message = "Request Time out."; 
       } else if (e == 'abort') 
       { 
        message = "Request was aborted by the server"; 
       } else 
       { 
        message = "Unknow Error."; 
       } 

       alert(message); 
      } 
     });