2

我有一些很棒的代码,可以很好地与 Wordpress 配合使用。它基本上向自定义循环添加了一个加载更多按钮,单击时会在下面添加下一批帖子。

我遇到的问题是这行代码取自以下代码段: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);
4

1 回答 1

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');
        }
    },

但这只是一个猜测,因为我不知道响应的样子。

于 2013-03-15T12:10:18.210 回答