如何使用ajax

时间:2017-10-17 11:00:40

标签: php ajax

我点击按钮后运行此功能

function Wo_GetMorePosts() {

    var query = $(".search-for-posts").val()

    if (search_status == "1"){

        Wo_SearchForPosts(query,1)

    }else{
        $.get(Wo_Ajax_Requests_File(), {
            f: 'posts',
            s: 'load_more_posts',
            filter_by_more: filter_by_more,
            after_post_id: after_post_id,
            user_id: user_id,
            page_id: page_id,
            group_id: group_id,
            posts_count: posts_count
        }, function (data) {
            if (data.length == 0) {
                $.get(Wo_Ajax_Requests_File(), {f: 'get_no_posts_name'}, function (data3) {
                    $('#load-more-posts').html('<div class="white-loading list-group"><div class="cs-loader"><div class="no-more-posts-to-show">' + data3.name + '</div></div>');
                });
            } else {
                $('#posts').append(data);
            }
        });

    }
    $('#load-more-posts').show();
    $('.loading-status').remove();
    Wo_progressIconLoader($('#load-more-posts'));
    scrolled = 0;
}

我的问题出在Wo_SearchForPosts(query,1)我想在每次点击后增加第二个参数(数字)。这样第二次点击就是Wo_SearchForPosts(query,2)

我该怎么做?

1 个答案:

答案 0 :(得分:0)

创建一个全局变量计数器,然后将计数器增加一个。

    var counter = 0;
    function Wo_GetMorePosts() {

        var query = $(".search-for-posts").val()

        if (search_status == "1"){
            Wo_SearchForPosts(query,++counter)
        }else{
            $.get(Wo_Ajax_Requests_File(), {
                f: 'posts',
                s: 'load_more_posts',
                filter_by_more: filter_by_more,
                after_post_id: after_post_id,
                user_id: user_id,
                page_id: page_id,
                group_id: group_id,
                posts_count: posts_count
            }, function (data) {
                if (data.length == 0) {
                    $.get(Wo_Ajax_Requests_File(), {f: 'get_no_posts_name'}, function (data3) {
                        $('#load-more-posts').html('<div class="white-loading list-group"><div class="cs-loader"><div class="no-more-posts-to-show">' + data3.name + '</div></div>');
                    });
                } else {
                    $('#posts').append(data);
                }
            });
        }
        $('#load-more-posts').show();
        $('.loading-status').remove();
        Wo_progressIconLoader($('#load-more-posts'));
        scrolled = 0;
    }