jQuery加载更多按钮

问题描述:

我有这个伟大的一点点的代码,Wordpress的工作得很好。它基本上为自定义循环添加了一个加载更多按钮,点击后添加下面的下一批帖子。jQuery加载更多按钮

我的问题是这行代码从下面的snippett采取:error: function() { jQuery('button.load-more').attr('disabled', 'disabled').text('No More Posts') } // Disable the button and change its contents when there are no more posts

基本上,当没有更多的职位,该按钮不按照指示获取禁用。有人可以帮我解决吗?这里的JS全:当服务器返回某种错误状态代码,这似乎并没有发生

var page = 1; 

loadMore = function() { 
    page++ //Increments the page number in the request 
    $.ajax({ 
     url: './page/' + page, 
     beforeSend: function() { 
      $('#wait').show().parent().find('button.load-more').hide(); 
     }, 
     complete: function() { 
      $('#wait').hide().parent().find('button.load-more').show(); 
     }, 
     success: function (data) { 
      var $data = $(data).find('article'); 
      // Replace div#the-posts with the name of your post container 
      jQuery('#posts .inner').append($data); 
     }, 
     error: function() { 
      jQuery('button.load-more').attr('disabled', 'disabled').text('No More Posts') 
     } // Disable the button and change its contents when there arew no more posts 

    }); // End Ajax 
}; // End loadMore 

jQuery('button.load-more').on('click', loadMore); 
+0

要禁用元素,请使用.prop而不是.attr。 – 2013-03-15 11:05:24

+0

@AleksandrM谢谢但不能解决问题 – egr103 2013-03-15 11:06:30

+0

脚本依赖请求返回一个404,当它遇到一个不存在的页码时。显然,服务器不这样做。 – JJJ 2013-03-15 11:06:54

误差函数只调用。

我的猜测是你需要检查响应。下面可以工作:

success: function (data) { 
     var $data = $(data).find('article'); 
     if ($data.length > 0) { 
      // Replace div#the-posts with the name of your post container 
      jQuery('#posts .inner').append($data); 
     } 
     else { 
      jQuery('button.load-more').attr('disabled', 'disabled').text('No More Posts'); 
     } 
    }, 

但这只是一种猜测,因为我不知道该怎么效应初探样子做。

+0

只是票。我在用IF语句跟踪答案时玩弄,但我弄错了。我是一个新手! – egr103 2013-03-15 17:43:08