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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作