喜欢反击javascript

时间:2017-08-22 19:39:12

标签: javascript php

我无法在javascript中运行我喜欢的计数器。 用完整的空心脏替换空心脏的工作正常,但计数器更新没有。有人能帮我吗?谢谢

$query2 = mysql_query("SELECT * FROM totlike WHERE id_user_like = $id_user AND id_post_like = ". $risultato['id'].""); 
                      if(mysql_num_rows($query2) ==1) {
        $like='<i class="glyphicon glyphicon-heart" id="'. $risultato['id'] .'"></i>';
                      } else {
                    $like='<i class="glyphicon glyphicon-heart-empty" id="'. $risultato['id'] .'"></i>';
                      }
echo '<div class="list" id="list_'. $risultato['id'] .'">
        <div class="panel panel-default">
      <div class="btn-group pull-right postbtn">
        <button type="button" class="dotbtn dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> <span class="dots"></span> </button>
        <ul class="dropdown-menu pull-right" role="menu">
          <li><a href="#myModal" role="button" data-toggle="modal" onclick="segnalazione('. $risultato['id'] .');">Segnala</a></li>
              '. $delete .'
        </ul>
      </div>
      <div class="col-md-12">
        <div class="question" id="question8025"><a href="profile.php?id='. $risultato['id_user'] .'" class="question-user"><div class="img imgLiquid imgLiquid_bgSize imgLiquid_ready" style="width: 60px; height: 60px; margin: 0px auto; background-image: url(&quot;download.jpg&quot;); background-size: cover; background-position: center center; background-repeat: no-repeat;"><img src="download.jpg" id="questionavatar" style="display: none;"></div></a>
          <div class="media-body">
            <h4 class="media-heading"><a href="profile.php?id='. $risultato['id_user'] .'" class="hashtag" rel="nofollow">'. $risultato['username'] .'</a><br>
              <small><i class="fa fa-clock-o"></i> Yesterday, 2:00 am</small> </h4>
            <p><h4><a href="post.php?id='. $risultato['id'] .'" class="hashtag" rel="nofollow">'. $status = nl2br($risultato['post']) .'</a></h4></p>
            <ul class="nav nav-pills pull-left ">
              <li><a href="javascript:void(0)" title="" onclick="like(' . $risultato["id"] . ','. $risultato['total_like'] .' );" id="'. $risultato["id"] .'">'.$like.' '.$risultato['total_like'].'</a></li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    </div>';

然后

function like(id_post, tot_like) {

  $.ajax({
       type: "POST",
       url: '../ajax/like.php',
       data:{id_post:id_post},
       complete: function() {
           var elem = $('#'+id_post).attr('class');
           if (elem=="glyphicon glyphicon-heart") {
               $('#'+id_post).removeClass('glyphicon glyphicon-heart').addClass('glyphicon glyphicon-heart-empty');
               $('#'+id_post).text(tot_like - 1);
            } else {
               $('#'+id_post).removeClass('glyphicon glyphicon-heart-empty').addClass('glyphicon glyphicon-heart');
               $('#'+id_post).text(tot_like + 1);
            }
       }
  });

当我点击计数器时转到-1或+2

1 个答案:

答案 0 :(得分:0)

您必须在更新后通过单击+1或-1进行刷新,而不是这样做。 你也必须做++或 - 这会更好。 1º获取总喜欢,2º显示它们直到现在,3º更新用户操作,4º再次刷新o只更新DOM对象+1或-1但是会很无用且很快就会出现一些错误或错误。