实现喜欢功能的问题。功能仅适用

时间:2016-11-07 00:05:55

标签: javascript jquery

喜欢或不喜欢的功能只运行一次,好像“喜欢”变量不再是真或假,这将是有效值。第一次它工作正常(喜欢和不喜欢),但第二次出现“其他”警报,这对我来说是绝对不清楚的。你能解释那里可能有什么问题吗?当我通过警告检查时,html中的Bool正确更新。

带有少量django模板代码的HTML,请不要注意:

<div class="incriminate_like" data-post-pk="{{ answer.pk }} " data-who-likes-id="{{ request.user.id }} " >

<div class="data_div" data-bool="false">
<img  class="like_image" src="{% static "img/like.png" %}"/>
</div>
</div>

JQUERY:

 $('.incriminate_like').click(function(){

  var post_pk = $(this).data("post-pk");
  var who_likes_id = $(this).data("who-likes-id");
  var that = $(this);
  var liked = $(this).find(".data_div").data("bool");

  function makeLiked(){

              that.find("img").attr("src","{% static 'img/likered.png' %}");
              that.find(".data_div").data("bool","true");
               // just incriminating 
              var like_number_original = that.next().html();
              var integer_of_like_number_original = parseInt(like_number_original);
              var plus_one_number = integer_of_like_number_original + 1
              that.next().html(plus_one_number);

              }

  function makeDisliked(){

              that.find("img").attr("src","{% static 'img/like.png' %}");
              that.find(".data_div").data("bool","false");
              // just incriminating 
              var like_number_original = that.next().html();
              var integer_of_like_number_original = parseInt(like_number_original);
              var plus_one_number = integer_of_like_number_original - 1
              that.next().html(plus_one_number);


    }

    if (liked == false) {
                          ajaxPost('/like/', {'post_pk': post_pk,'who_likes_id':who_likes_id,'whom_id':whom_id}, function(){
                          makeLiked();


                                                                                                                           })
                         }


    else if (liked == true ) {

                    ajaxPost('/dislike/', {'post_pk': post_pk,'who_likes_id':who_likes_id,'whom_id':whom_id}, function(){
                    makeDisliked();



                                                                                                                        })




                             }



    else {

                      alert('error');
        }

                                  })

1 个答案:

答案 0 :(得分:0)

问题是,如果你调用makeLiked()或makeDisliked(),你将data-bool值设置为string,这就是为什么like == true(甚至喜欢== false)返回的结果为false。 尝试设置如下内容:

$('.incriminate_like').click(function() {

  var post_pk = $(this).data("post-pk");
  var who_likes_id = $(this).data("who-likes-id");
  var that = $(this);
  var liked = $(this).find(".data_div").data("bool");

  function makeLiked() {

    that.find("img").attr("src", "{% static 'img/likered.png' %}");
    that.find(".data_div").data("bool", true);
    // just incriminating 
    var like_number_original = that.next().html();
    var integer_of_like_number_original = parseInt(like_number_original);
    var plus_one_number = integer_of_like_number_original + 1
    that.next().html(plus_one_number);

  }

  function makeDisliked() {

    that.find("img").attr("src", "{% static 'img/like.png' %}");
    that.find(".data_div").data("bool", false);
    // just incriminating 
    var like_number_original = that.next().html();
    var integer_of_like_number_original = parseInt(like_number_original);
    var plus_one_number = integer_of_like_number_original - 1
    that.next().html(plus_one_number);


  }

  if (liked == false) {
    ajaxPost('/like/', {
      'post_pk': post_pk,
      'who_likes_id': who_likes_id,
      'whom_id': whom_id
    }, function() {
      makeLiked();


    })
  } else if (liked == true) {

    ajaxPost('/dislike/', {
      'post_pk': post_pk,
      'who_likes_id': who_likes_id,
      'whom_id': whom_id
    }, function() {
      makeDisliked();



    })




  } else {

    alert('error');
  }

})

(注意第二个参数没有“”)

所以最终的代码应该是这样的:

m