2013-04-30 53 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); 
      } 
     });