doufen3786 2015-12-21 12:14
浏览 31
已采纳

AJAX成功无法正确更新

I have the following code which I am using for a simple LIKE system on my site. The problem I am having is that if a user clicks like, the css style changes as it should, but if the user clicks it again it should then unlike. This is not happening, if the like is clicked twice it likes it twice rather than like/unlike.

The JS:

// ADD THE LIKE
$(function() {
    $(".like").click(function() {
        var item_id = $(this).attr("id");
        $('a#' + item_id).removeClass("like");
        $.ajax({
            type: "POST",
            url: "statAdd.php",
            data: "item_id=" + item_id + "&uname=" + "<?php echo $memName; ?>" + "&uID=" + "<?php echo $memID; ?>" + "&statType=" + "like",
            cache: false,
            success: function(data) {
                $('a#' + item_id).addClass("liked");
                $('a#' + item_id).html(data);
            }
        });
        return false;
    });
});

// REMOVE THE LIKE
$(function() {
    $(".liked").click(function() {
        var item_id = $(this).attr("id");
        $('a#' + item_id).removeClass("liked");
        $.ajax({
            type: "POST",
            url: "statAdd.php",
            data: "item_id=" + item_id + "&uname=" + "<?php echo $memName; ?>" + "&uID=" + "<?php echo $memID; ?>" + "&statType=" + "liked",
            cache: false,
            success: function(data) {
                $('a#' + item_id).addClass("like");
                $('a#' + item_id).html(data);
            }
        });
        return false;
    });
});

The AJAX server code returns the following data on success:

// ADD LIKE
echo '<i class="fa fa-heart"></i> <span>'.$number.' Likes including you</span>';

// REMOVE LIKE
echo '<i class="fa fa-heart"></i> <span>'.$number.' Likes</span>';

This is the HTML:

<?php
// Check if the user has liked the post previously.
    if(mysqli_num_rows($qryLikes) == 0){
        echo '<a href="javascript:void();" title="LIKE THIS" class="like" id="'.$statRow['statID'].'"><i class="fa fa-heart"></i> <span>'.$likes.' Likes</span></a>';
    } else {
        echo '<a href="javascript:void();" title="UNLIKE THIS" class="liked" id="'.$statRow['statID'].'"><i class="fa fa-heart"></i> <span>'.$likes.' Likes including you</span></a>';
    }
    ?>

Is there anyone out there that can point me to my error?

Kind regards

  • 写回答

1条回答 默认 最新

  • douzhang2092 2015-12-21 12:21
    关注

    Currently what you are using is called a "direct" binding which will only attach to element that exist on the page at the time your code makes the event binding call.

    You need to use Event Delegation using .on() delegated-events approach, when generating elements dynamically or manipulation elements (like removing and adding ).

    General Syntax

    $(document).on('event','selector',callback_function)
    

    Example

    $(function() {
        // ADD THE LIKE
        $(document).on('click',".like", function() {
            //Existing code
        });
    
        // REMOVE THE LIKE
        $(document).on('click',".liked", function() {
            //Existing code
        });
    });
    

    In place of document you should use closest static container.

    The delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, we can use delegated events to bind the click event to dynamically created elements and also to avoid the need to frequently attach and remove event handlers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法