javascript - jQuery 加载更多按钮

标签 javascript jquery load

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

我遇到的问题是从下面的代码片段中获取的这行代码: error: function() { jQuery('button.load-more').attr('disabled', 'disabled')。 text('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);

最佳答案

只有当服务器返回某种错误状态代码时才会调用错误函数,但这似乎不会发生。

我的猜测是您需要检查响应。以下方法可能有效:

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

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

关于javascript - jQuery 加载更多按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15430724/

相关文章:

javascript - 如果无法在 loadData() Controller 中使用 AJAX,如何更新 jsGrid 中的数据? (数组尚未完成填充)

jquery - 高级 Jquery 动画

jquery - 滚动查看更多脚本——如何?

javascript - IE 和 jQuery load() 的问题

java - 性能测试 Swing 应用程序

javascript - 保证不冒泡外

javascript,搜索我的名字

javascript - JavaScript 中的提前退出模块化

Javascript:使用原型(prototype)描述符添加方法

javascript - Jquery内容 slider : Add an error class to a navigation menu that corresponds to empty section