如何增加喜欢和不喜欢

时间:2016-06-13 09:15:48

标签: javascript php jquery

我在每个帖子上都有一个按钮

1)喜欢按钮

如果用户已经喜欢该帖子,则显示与按钮不同

如果用户不喜欢帖子

,这里是HTML部分
<a href="javascript:void(0);" id="liker" data-count="0" data-fpc="481" data-id="cDJod1RnUU45eXBQWmp">Like</a>

如果用户已经喜欢帖子

<a href="javascript:void(0);" id="unliker" data-count="1" data-fpc="481" data-id="cDJod1RnUU45eXBQWmp">Unlike</a>

这里是jquery部分,这部分发送类似请求

$(function() {
    $("body").on("click", "#liker", function() {
        var t = $(this).data("id"),
            a = $(this).data("count"),
            f = $(this).data("fpc");
        a++;
        $("#cyli" + f).text(a);
        $(this).attr('id', 'unliker');
        $(this).data('count', a);
        $(this).text('Unlike');
        i = this, $.ajax({
            type: "POST",
            url: "/ajax/liker?io=" + t,
            data: "id=" + t,
            success: function(t) {}
        })
    });
});

下面的部分发送不像请求

$(function() {
    $("body").on("click", "#unliker", function() {
        var t = $(this).data("id"),
            a = $(this).data("count"),
            f = $(this).data("fpc");
        a -= 1;
        $("#cyli" + f).text(a);
        $(this).attr("id", "liker");
        $(this).data('count', a);
        $(this).text("Like");
        i = this, $.ajax({
            type: "POST",
            url: "/ajax/unliker?io=" + t,
            data: "id=" + t,
            success: function(t) {}
        })
    });
});

此标记<i class="fa fa-thumbs-up" id="cyli481" aria-hidden="true">&nbsp; 0</i>

中显示的值与此类似

现在的问题是,当我点击时,或者不同的是,没有正确添加或扣除该值。

例如,如果data-count =“0”,这意味着此帖子上没有相似内容,但是当我点击它时会自动变为2然后如果我点击不像按钮它会变为-1,上面的方法可能会出错

1 个答案:

答案 0 :(得分:0)

只需将$(this).data("count")$(this).data("count", a)更改为$(this).attr("data-count")$(this).attr("data-count", a)即可。

试试这段代码:

&#13;
&#13;
$(function() {
    $("body").on("click", "#liker", function() {
        var t = $(this).data("id"),
            a = $(this).attr("data-count"),
            f = $(this).data("fpc");
        a++;
        $("#cyli" + f).text(a);
        $(this).attr('id', 'unliker');
        $(this).attr('data-count', a);
        $(this).text('Unlike');
        i = this, $.ajax({
            type: "POST",
            url: "/ajax/liker?io=" + t,
            data: "id=" + t,
            success: function(t) {}
        })
    });
});
$(function() {
    $("body").on("click", "#unliker", function() {
        var t = $(this).data("id"),
            a = $(this).attr("data-count"),
            f = $(this).data("fpc");
        a -= 1;
        $("#cyli" + f).text(a);
        $(this).attr("id", "liker");
        $(this).attr('data-count', a);
        $(this).text("Like");
        i = this, $.ajax({
            type: "POST",
            url: "/ajax/unliker?io=" + t,
            data: "id=" + t,
            success: function(t) {}
        })
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="javascript:void(0);" id="liker" data-count="0" data-fpc="481" data-id="cDJod1RnUU45eXBQWmp">Like</a>
<i class="fa fa-thumbs-up" id="cyli481" aria-hidden="true">&nbsp; 0</i>
&#13;
&#13;
&#13;

相关问题