首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >当回波跟随Ajax按钮在Ajax分页模板不工作?

当回波跟随Ajax按钮在Ajax分页模板不工作?
EN

WordPress Development用户
提问于 2019-02-22 11:30:56
回答 1查看 155关注 0票数 0

好吧,这次我会尽量说清楚的,我真的需要你们的帮助。

我使用一个与Ajax一起工作的插件来创建一个“跟随”按钮,让我们的用户互相跟踪。

我把下面的按钮放在我需要一个用户跟踪其他用户的任何地方,就像那个echo pwuf_get_follow_unfollow_links( //user id here );

我面临的问题是,如果我在任何Ajax分页模板中添加了这个回显函数--无论是在Ajax分页后还是用户列出Ajax分页,下面的按钮都不能工作,并且在单击时找不到用户id!

但是可以肯定的是,在我将更多信息加载到第二个页面之前,他可以在第一个页面上找到用户id!

例句:如果我有一个类似于这个Ajax的分页,并且试图在这个ajax分页中回显下面的按钮函数,那么这个页面就不能工作了!?

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
add_action('wp_ajax_loadmore_by_ajax', 'loadmore_by_ajax_callback');
add_action('wp_ajax_nopriv_loadmore_by_ajax', 'loadmore_by_ajax_callback');

function loadmore_by_ajax_callback()  {
   //do something

   //echoing follow button
    echo pwuf_get_follow_unfollow_links( //user id here );

}

我将在下面添加我的所有插件代码,我希望得到一些帮助来解决这个问题,谢谢您的时间。

follow.js

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
jQuery(document).ready(function($) {
    /*******************************
    follow / unfollow a user
    *******************************/
    $( '.follow-links a' ).on('click', function(e) {
        e.preventDefault();

        var $this = $(this);

        if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
            alert( pwuf_vars.login_required );
            return;
        }

        var data      = {
            action:    $this.hasClass('follow') ? 'follow' : 'unfollow',
            user_id:   $this.data('user-id'),
            follow_id: $this.data('follow-id'),
            nonce:     pwuf_vars.nonce
        };

        $this.closest('.follow-links').find('img.pwuf-ajax').show();

        $.post( pwuf_vars.ajaxurl, data, function ( response ) {
            if ( response == 'success' ) {
                if ( $this.hasClass( 'follow' ) ) {;
                    $this.removeClass( 'follow' ).addClass( 'unfollow' );
                    $this.find( 'span' ).text( 'Unfollow' );
                } else {;
                    $this.removeClass( 'unfollow' ).addClass( 'follow' );
                    $this.find( 'span' ).text( 'Follow' );
                }
            } else {
                alert( pwuf_vars.processing_error );
            }
            $this.closest('.follow-links').find('img.pwuf-ajax').hide();
        });
    });
});

actions.php

代码语言:javascript
代码运行次数:0
运行
复制

display-functions.php

代码语言:javascript
代码运行次数:0
运行
复制

follow-functions.php

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 $follow ) {
            if ( $follow == $unfollow_user ) {
                unset( $following[$key] );
                $modified = true;
            }
        }

        if ( $modified ) {
            if ( update_user_meta( $user_id, '_pwuf_following', $following ) ) {
                pwuf_decrease_followed_by_count( $unfollow_user );
            }
        }

    }

    // get all IDs that follow the user we have just unfollowed so that we can remove $user_id
    $followers = pwuf_get_followers( $unfollow_user );

    if ( is_array( $followers ) && in_array( $user_id, $followers ) ) {

        $modified = false;

        foreach ( $followers as $key => $follower ) {
            if ( $follower == $user_id ) {
                unset( $followers[$key] );
                $modified = true;
            }
        }

        if ( $modified ) {
            update_user_meta( $unfollow_user, '_pwuf_followers', $followers );
        }

    }

    if ( $modified ) {
        do_action( 'pwuf_post_unfollow_user', $user_id, $unfollow_user );
        return true;
    }

    return false;
}



/**
 * Retrieve following count
 *
 * Gets the total number of users that the specified user is following
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve a count for
 * @return      int
 * @param  int|string|WP_User $user_id User ID or object.
 * @return bool                        Whether the user exists
 */

function pwuf_get_following_count( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $following = pwuf_get_following( $user_id );

    $count = 0;

    if ( $following ) {
        $count = count( $following );
    }

    return (int) apply_filters( 'pwuf_get_following_count', $count, $user_id );
}


/**
 * Retrieve follower count
 *
 * Gets the total number of users that are following the specified user
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve a count for
 * @return      int
 */

function pwuf_get_follower_count( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $followed_count = get_user_meta( $user_id, '_pwuf_followed_by_count', true );

    $count = 0;

    if ( $followed_count ) {
        $count = $followed_count;
    }

    return (int) apply_filters( 'pwuf_get_follower_count', $count, $user_id );
}



/**
 * Increase follower count
 *
 * Increments the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to increease the count for
 * @return      int
 */

function pwuf_increase_followed_by_count( $user_id = 0 ) {

    do_action( 'pwuf_pre_increase_followed_count', $user_id );

    $followed_count = pwuf_get_follower_count( $user_id );

    if ( $followed_count !== false ) {

        $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', $followed_count + 1 );

    } else {

        $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', 1 );

    }

    do_action( 'pwuf_post_increase_followed_count', $user_id );

    return $new_followed_count;
}


/**
 * Decrease follower count
 *
 * Decrements the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to decrease the count for
 * @return      int
 */

function pwuf_decrease_followed_by_count( $user_id ) {

    do_action( 'pwuf_pre_decrease_followed_count', $user_id );

    $followed_count = pwuf_get_follower_count( $user_id );

    if ( $followed_count ) {

        $count = update_user_meta( $user_id, '_pwuf_followed_by_count', ( $followed_count - 1 ) );

        do_action( 'pwuf_post_increase_followed_count', $user_id );

    }
    return $count;
}


/**
 * Check if a user is following another
 *
 * Increments the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id       - the ID of the user doing the following
 * @param   int $followed_user - the ID of the user to check if being followed by $user_id
 * @return      int
 */

function pwuf_is_following( $user_id, $followed_user ) {

    $following = pwuf_get_following( $user_id );

    $ret = false; // is not following by default

    if ( is_array( $following ) && in_array( $followed_user, $following ) ) {
        $ret = true; // is following
    }

    return $ret;

}

scripts.php

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 __( 'There was a problem processing your request.', 'pwuf' ),
        'login_required'   => __( 'Oops, you must be logged-in to follow users.', 'pwuf' ),
        'logged_in'        => is_user_logged_in() ? 'true' : 'false',
        'ajaxurl'          => admin_url( 'admin-ajax.php' ),
        'nonce'            => wp_create_nonce( 'follow_nonce' )
    ) );
}
add_action( 'wp_enqueue_scripts', 'pwuf_load_scripts' );
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2019-02-22 12:04:10

我认为,如果您更改您的jQuery单击操作选择器来处理事件委托,那么下面的按钮在ajax添加之后应该可以工作。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// Change this
$( '.follow-links a' ).on('click', function(e) {
// to this
$( '.some-parent-not-added-by-ajax' ).on('click', '.follow-links a', function(e) {
// or as last resort
$( document ).on('click', '.follow-links a', function(e) {
票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/329632

复制
相关文章
Ajax工作流程(原生Ajax)
(1)初始化XMLHttpRequest对象。不同浏览器的差异,需要我们创建一个跨浏览器的对象,并判断XMLHttpRequest对象创建是否成功,如果不成功,则给予提示。
红目香薰
2022/11/29
7890
AJAX 工作原理
AJAX 全称为“Asynchronous JavaScript and XML”(异步 JavaScript 和 XML),是一种创建交互式网页应用的网页开发技术。它使用:
李玺
2021/11/22
1K0
AJAX 工作原理
wordpress实现 ajax 分页加载
由于我们可以在后台使用wp query来输出文章列表,所以我们并不需要文章分页的入口,砍掉了分页入口也避免了搜索引擎抓取这些页面。我们只需要在AJAX 执行的过程中向后台传递一个分页参数,就可以返回这个分页上的文章列表。再返回文章列表的时候,我们还需要返回下一分页的页码,当然如果不是最后一页的话。
李维亮
2021/07/09
1.3K0
spring mvc+ajax分页[通俗易懂]
分页大致思路:页面每次把当前页传到后台并获得从后台传过来的json数据,解析后布局到这个页面上。
全栈程序员站长
2022/07/10
7910
spring mvc+ajax分页[通俗易懂]
Ajax方式分页加载列表实现
  最近需要用到这个功能,所以这几天一直在研究这个,目前大致功能已实现,后续需要完善,但需要的功能点已完成,记录下;
JQ实验室
2022/02/09
3.5K0
AJAX 下拉无刷新分页加载
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011415782/article/details/71641379
泥豆芽儿 MT
2018/09/11
4.9K0
AJAX 下拉无刷新分页加载
原生js+ajax分页组件
定义分页组件DOM <div id="pagination" class="pagination"></div> 定义分页组件类及实例方法: // 分页组件类 function Pagination(_ref) { this.id = _ref.id; //分页组件挂载的DOM节点 this.curPage = _ref.curPage || 1; //初始页码 this.draw = _ref.draw; // 初始化分页请求次数 this.pageSi
前端_AWhile
2022/05/10
7.7K0
jQuery ajax - ajax() 方法jQuery ajax - ajax() 方法
http://www.w3school.com.cn/jquery/ajax_ajax.asp
一个会写诗的程序员
2018/08/17
14.5K0
Django 分页和使用Ajax5.3
分页 Django提供了一些类实现管理数据分页,这些类位于django/core/paginator.py中 Paginator对象 Paginator(列表,int):返回分页对象,参数为列表数据,每面数据的条数 属性 count:对象总数 num_pages:页面总数 page_range:页码列表,从1开始,例如[1, 2, 3, 4] 方法 page(num):下标以1开始,如果提供的页码不存在,抛出InvalidPage异常 异常exception InvalidPage:当向page()传入一个
Lansonli
2021/10/09
3K0
Ajax工作原理及概述
AJAX是异步的JavaScript和XML(Asynchronous JavaScript And XML)。简单点说,就是使用 XMLHttpRequest 对象与服务器通信。 它可以使用JSON,XML,HTML和文本等多种格式发送和接收。AJAX最吸引人的就是它的“异步”特性,也就是说他可以在不重新刷新页面的情况下与服务器通信,交换数据,更新页面。
用户1093975
2018/08/03
9120
SUI分页组件和avalon搞定ajax无刷新分页
<div ms-controller="main"> <h2 class="pagination-centered">{{ title }}</h2> <form method="get" action="" class="sui-form" style="margin-bottom:5px;"> 重量:<input class="input-medium" type="text" name="weight" value="@ViewBag.weight" id="weigh
阿炬
2018/05/11
1.6K0
jQuery ajax - ajax()方法
什么是 AJAX? AJAX = 异步 JavaScript 和 XML(Asynchronous JavaScript and XML)。
江一铭
2022/06/17
9.4K0
WordPress中通过Ajax评论分页实现方法
一直看着评论一线到底,感觉有点不舒服,看到主题君欲思大大那的评论也分页了,就向大大求援了一下,大大酷酷的回了一句paginate_comments_links函数,剩下的就都留给小弟了。才一开始光搜那个函数,始终找不到自己想要的,偶然间灵光一闪,想到之前comments.php中找到的那个
WindCoder
2018/09/19
1.3K0
Ajax之二 Ajax基础
Ajax是一组技术的集合,Asp.Net AJAX就是建立在这组技术的基础之上的。虽然Asp.Net AJAX尽量隐藏了Ajax的技术细节,但是要想明白Asp.Net AJAX能干什么,或者说要想扩展这个框架以便创建所需要的高级应用程序,那么就必须拥有关于Ajax的一些更加深入的知识。
用户9184480
2024/12/17
970
Ajax之二   Ajax基础
AJAX学习(一)AJAX基础
ajax在网络应用开发上运用很广泛,它能够达到局部刷新的效果,也就是页面的某一个组件或功能上进行客户端和服务端的数据交互来实现数据的刷新,而不需要整个页面重载,这样可以提升用户的使用感,缩短等待的时间。 ajax的可以用的地方很多,因此是一个很重要的知识点。所以在此写下有关于我对ajax的学习的感悟和应用的一些实例和大家分享,也希望自己对它能够更加了解
全栈程序员站长
2022/11/02
1.5K0
AJAX学习(一)AJAX基础
ajax html例子,AJAX实例[通俗易懂]
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
全栈程序员站长
2022/09/22
2.5K0
ajax html例子,AJAX实例[通俗易懂]
Ajax:初次认识ajax,ajax使用方法
我们来个简单的测试,使用最原始的HttpServletResponse处理 , .最简单 , 最通用
冷环渊
2021/10/19
5.8K0
AJAX读音_ajax怎么发音
AJAX全称”Asynchronous JavaScript and XML”(异步JavaScript和XML)
全栈程序员站长
2022/09/19
4.4K0
【自然框架】QuickPager asp.net 分页控件的Ajax分页方式。
  上次比较匆忙,Ajax的分页方式仅实现了基本功能,或者说只是验证了我的想法。现在对Ajax分页有做了一些调整,现在可以正式用了。   使用方法还是非常简单,可以完全按照URL分页方式来做,只需要把PagerTurnKind 属性 设置为:PagerTurnKind.AjaxForWebControl;就可以了。   然后前台需要引用jquery-1.4.2.min.js和QuickPager-1.0.js。QuickPager-1.0.js是QuickPager需要用的一个js脚本,Demo里面有这个j
用户1174620
2018/02/26
1.8K0
Ajax工作原理及实例「建议收藏」
  ajax 的全称是Asynchronous JavaScript and XML,其中,Asynchronous 是异步的意思,它有别于传统web开发中采用的同步的方式。
全栈程序员站长
2022/09/06
6750
Ajax工作原理及实例「建议收藏」

相似问题

Ajax过滤分页

10

AJAX前端按钮单击“不工作”-自定义插件

10

Wordpress在定制模板上的分页不工作

10

重定向在ajax函数中不工作。

10

ajax -点的WordPress分页

10
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文