所以我的评论有一个喜欢按钮,工作正常,但我需要一点增强。这是下面的代码
<script type="text/javascript">
$(document).ready(function(){
$(".voteMe").click(function() {
var voteId = this.id;
var all_id = voteId.split(' ');
var cid = voteId.split('_');
var id_values = all_id[1].split('_');
var upOrDown = id_values[0];
var user_id = id_values[1];
var topic_id = id_values[2];
var uri = "http://localhost/forum/topic/votes/" + cid[0] + '/' + upOrDown + '/' + user_id + '/' +topic_id;
$.ajax({
type: "post",
url: uri,
cache: false,
success: function(response){
try{
if(response === 'true'){
var newValue = parseInt($("#"+all_id[0]+'_result').text()) + 1;
$("#"+all_id[0]+'_result').html(newValue);
}else{
alert("unable to update");
}
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
});
});
</script>
所以我想要的是,当用户单击喜欢按钮时,它应该被禁用,当不喜欢按钮被同一用户单击时,-1 将从喜欢按钮中减去,然后启用喜欢按钮并禁用不喜欢。
这是喜欢和不喜欢的 html 代码。我知道这要求太多了,如果这样做,我将不胜感激。
<div class="likeblock pull-left">
<a id="<?= $comment->id ?>_upvote up_<?= $comment->user_id?>_<?= $comment->topic_id?>" class="up voteMe">
<i class="fa fa-thumbs-o-up"></i>
<span id="<?= $comment->id ?>_upvote_result" ><?=$comment->up_vote_count?></span>
</a>
<a id="<?= $comment->id ?>_downvote down_<?= $comment->user_id?>_<?= $comment->topic_id?>" class="down voteMe">
<i class="fa fa-thumbs-o-down"></i>
<span id="<?= $comment->id ?>_downvote_result" ><?=$comment->down_vote_count?></span>
</a>
</div>
提前谢谢。
我认为以下部分需要关注,如果和否则似乎很相似
if(response === 'true'){
var newValue = parseInt($("#"+all_id[0]+'_result').text()) + 1;
$("#"+all_id[0]+'_result').html(newValue);
}else{
var newValue = parseInt($("#"+all_id[0]+'_result').text()) + 1;
$("#"+all_id[0]+'_result').html(newValue);
}