Javascript 需要Jquery ajax切换的帮助吗
因此,我有一个类似的按钮,我的评论的,工作良好,但我需要一点加强它。下面是代码Javascript 需要Jquery ajax切换的帮助吗,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,因此,我有一个类似的按钮,我的评论的,工作良好,但我需要一点加强它。下面是代码 <script type="text/javascript"> $(document).ready(function(){ $(".voteMe").click(function() { var voteId = this.id; var all_id = voteId.split(' '); v
<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>
$(文档).ready(函数(){
$(“.voteMe”)。单击(函数(){
var voteId=this.id;
var all_id=voteId.split(“”);
变量cid=voteId.split(“”);
var id_values=all_id[1]。拆分(“”);
var upOrDown=id_值[0];
var user_id=id_值[1];
var topic_id=id_值[2];
var uri=”http://localhost/forum/topic/votes/“+cid[0]+'/'+upOrDown+'/'+user_id+'/'+topic_id;
$.ajax({
类型:“post”,
url:uri,
cache:false,
成功:功能(响应){
试一试{
如果(响应=='true'){
var newValue=parseInt($(“#”+all_id[0]+“_result”).text()+1;
$(“#”+all_id[0]+“_result').html(newValue);
}否则{
警报(“无法更新”);
}
}捕获(e){
警报(“请求时出现异常…”;
}
},
错误:函数(){
警报(“请求时出错…”;
}
});
});
});
所以我想要的是当一个用户点击like按钮时,它应该被禁用,当同一个用户点击厌恶按钮时,从like按钮中减去1,然后like按钮被启用,厌恶按钮被禁用
下面是喜欢和不喜欢的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和else看起来都很相似
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);
}
我在我的模型中做了一些修改,所以else块现在看起来像这个else{alert(“无法更新”);}还添加了条件来检查它是向上投票还是向下投票,并相应地更新span的值,以响应===true块