我正在尝试在我的网站上建立评论回复系统。我已经制作了所有的 php 文件,但我一直在这里更改投票数。
如图所示,在投票一次后,内容会完美更改,我可以根据需要将课程从upvote
toupvoted
或downvote
to更改为downvoted
或。
但问题是,一旦课程改变,内容也会改变。但我不能再次投票,直到页面重新加载。
在此代码之前,我正在使用addClass()
and更改类removeClass()
。
这也导致了同样的问题。
.html
<span class='votes'><span data-id='8' class='comment vote upvote'></span>".$row['downvotes']."</span>
myId 是数据 ID。如您所见,我正在使用数据更改变量 x 的 html 内容。
但是一旦改变,我就不能再投票了。
.js(jQuery)
$(document).ready(function(){
$('#notice').hide();
$("span").click(function(){
if($(this).hasClass('vote')){
var myId = $(this).attr('data-id');
var action = 'a';
if($(this).hasClass('upvote')){
action = 'upvote';
} else if($(this).hasClass('upvoted')){
action = 'upvoted';
} else if($(this).hasClass('downvote')){
action = 'downvote';
} else if($(this).hasClass('downvoted')){
action = 'downvoted';
}
var url;
var class1;
var class2;
if(action=='upvote'){
class1 = "upvoted";
class2 = "downvote";
}else if(action=='upvoted'){
class1 = "upvote";
class2 = "downvote";
}else if(action=='downvote'){
class1 = "upvote";
class2 = "downvoted";
}else if(action=='downvoted'){
class1 = "upvote";
class2 = "downvote";
}
var content;
/* If voted element is video */
if($(this).hasClass('video')){
url = base_url + 'forms/votes/vote_video.php';
content = 'video';
}
/* If voted element is comment */
else if($(this).hasClass('comment')){
url = base_url + 'forms/votes/vote_comment.php';
content = 'comment';
}
var x = $(this).parent();
if(action=='upvote'){
content1 = "<span data-id='"+ myId +"' class='"+ content +" vote upvoted'></span>";
content2 = " <span data-id='"+ myId +"' class='"+ content +" vote downvote'></span>";
}else if(action=='upvoted'){
content1 = "<span data-id='"+ myId +"' class='"+ content +" vote upvote'></span>";
content2 = " <span data-id='"+ myId +"' class='"+ content +" vote downvote'></span>";
}else if(action=='downvote'){
content1 = "<span data-id='"+ myId +"' class='"+ content +" vote upvote'></span>";
content2 = " <span data-id='"+ myId +"' class='"+ content +" vote downvoted'></span>";
}else if(action=='downvoted'){
content1 = "<span data-id='"+ myId +"' class='"+ content +" vote upvote'></span>";
content2 = " <span data-id='"+ myId +"' class='"+ content +" vote downvote'></span>";
}
$.post(url, { myId:myId, action:action }, function(data){
if(data=="error"){
alert("Error, your vote couldn't be counted.");
}
else{
x.html(content1+data.up+content2+data.down);
}
},"json");
}
});
});
请在这里帮助我。或有关如何votes
仅加载跨度的建议。