weixin_33670786 2015-07-24 11:45 采纳率: 0%
浏览 9

用ajax更改类

The script below changes the class from addFollow to removeFollow with ajax.

After clicking it should do the reverse - change the class from removeFollow to addFollow.

But for some reason I'm always getting the first class which was loaded with the page.

When I checked the code the class was changed. But the JS still getting the first class when the page loaded.

Why is that and how can I fix that?

// check if already following
if (is_following ($_SESSION['userDetails']['userID'], $_GET['ownerID']) )
{
    $follow_btnColor = "btn-primary";
    $follow_act = "removeFollow";
}
else
{
    $follow_btnColor = "btn-default";
    $follow_act = "addFollow";
}

LINK:

<a href="#" id="follow" class="btn btn-lg <?PHP echo $follow_btnColor ?> <?PHP echo $follow_act ?>" ownerID="<?PHP echo $_GET['ownerID'] ?>" ><i class="fa fa-paw"></i>  FOLLOW</a>

JS:

$(function() {
    $(".addFollow").click(function(){
        var element = $(this);

         $.ajax({
            type: "POST",
            url: "ajax/follow.php",
            data: info,
            success: function(){
            $("#loading").ajaxComplete(function()
            {
            }).slideUp();

            $(element).toggleClass( "addFollow , removeFollow" );   
            $(element).toggleClass( "btn-primary , btn-default" );  

           }
         });

        return false;
    });
});


$(function() {
    $(".removeFollow").click(function(){
        var element = $(this);

         $.ajax({
           type: "POST",
           url: "ajax/follow.php",
           data: info,
           success: function(){
               $("#loading").ajaxComplete(function()
                 {}).slideUp();

                $(element).toggleClass( "removeFollow , addFollow" );
                $(element).toggleClass( "btn-default , btn-primary" );
           }
         });

        return false;

    });

});
  • 写回答

1条回答 默认 最新

  • MAO-EYE 2015-07-24 11:50
    关注
    Try this in your js code
    element.toggleClass( "removeFollow , addFollow" );
    element.toggleClass( "btn-default , btn-primary" );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)