我在 jQuery 上有这个问题投票系统,它非常有效:
$(".q_upvote").click(function()
{
var vote = "vote_up";
var question_id = this.id.split('_')[1];
var votedata = "q_vote=" + vote+"&question_id="+question_id;
$.ajax({
type: 'POST',
url: 'vote.php',
data: votedata,
success: function(vote_msg){
if($.trim(vote_msg) == 'ok')
{
$("#votenum").text( parseInt($("#votenum").text()) + 1 ); }
else{
}
}
});
}
)
这是它的 HTML 部分:
<p><span id="votenum">{$question_vote}</span>
<a id="upvote_{$qid}" class="q_upvote" href="#"><i class="icon-thumbs-up"></i></a>
我也想对答案做同样的事情。我写了这个:
$(".a_upvote").click(function()
{
var vote = "vote_up";
var answer_id = this.id.split('_')[1];
votedata = "q_vote=" + vote+"&answer_id="+answer_id;
$.ajax({
type: 'POST',
url: 'vote2.php',
data: votedata,
success: function(vote_msg){
alert(vote_msg)
if($.trim(vote_msg)== 'ok')
{
$("#answervotes").text( parseInt($("#answervotes").text()) + 1 );
}
else{
}
}
});
}
)
这是它的 HTML 部分:
{if isset($allanswers) eq True} {section name=res loop=$allanswers}</p>
<div text-align:center;><p> {$allanswers[res].answer_text} </p>
{if isset($smarty.session.username) eq True}
<p><span id = "answervotes">{$allanswers[res].answer_total_rate}</span>
<a id="upvote_{$allanswers[res].answer_id}" class="a_upvote" href="#""><i class="icon-thumbs-up"></i></a>
<a id="downvote_{$allanswers[res].answer_id}" class="a_downvote" href="#" "><i class="icon-thumbs-down" ></i></a></p>
{else}
<p>{$allanswers[res].answer_total_rate}</p>
{/if}
我的问题是,如果有多个答案并且用户支持第 n 个答案,则只有第一个数字的投票发生变化,第 n 个答案的投票不会改变。我认为我需要以某种方式使答案投票独一无二,但我不知道如何。我怎样才能解决这个问题?谢谢。