jQuery .on()在ajax成功

时间:2013-01-30 10:29:21

标签: jquery ajax events

我想将.on()绑定到ajax“事件”,这意味着每当成功检索到ajax响应时,我都希望它被触发。 我不知道如何将.on()绑定到这样的事件上。我是否需要像这样使用load

$(document).on('load','#elementInsertedByAjax',function(){
    // will do something
});

PS我确实需要使用.on(),因为整个页面是通过ajax调用插入的。

3 个答案:

答案 0 :(得分:9)

您可以使用.ajaxSuccess()

$(document).ajaxSuccess(function() {
// will do something
});

或绑定到

$(document).on('ajaxSuccess','#elementInsertedByAjax',function(){
    // will do something
});

答案 1 :(得分:1)

您可以使用ajax全局事件jQuery.ajaxSuccess

$(document).ajaxSuccess(function() {
    $( ".log" ).text( "Triggered ajaxSuccess handler." );
});

答案 2 :(得分:0)

$,ajax({
  url:'yoururl',
  type : 'GET', /*or POST*/
  data: {'d1',data1}, /*if any*/
  success:function(returndatafromserver){
         //here you can check the success of ajax
  },error:function(errormsg){

   //error if any

  }

});