2013-03-15 79 views
2

我有這個偉大的一點點的代碼,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

回答

2

誤差函數只調用。

我的猜測是你需要檢查響應。下面可以工作:

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