使用Jquery和Smarty在HTML中具有唯一的span id
我在jQuery上有一个问题投票系统,它可以完美地工作:使用Jquery和Smarty在HTML中具有唯一的span id,jquery,html,smarty,Jquery,Html,Smarty,我在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({
$(".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>
{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}
这是它的HTML部分:
<p><span id="votenum">{$question_vote}</span>
<a id="upvote_{$qid}" class="q_upvote" href="#"><i class="icon-thumbs-up"></i></a>
{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}
{if isset($allanswers)eq True}{section name=res loop=$allanswers}
{$allanswers[res].答案_text}
{if isset($smarty.session.username)eq True}
{$allanswers[res].答案_总_比率}
{else}
{$allanswers[res].答案_总_比率}
{/if}
我的问题是,如果有多个答案,用户向上投票第n个答案,只有第一个数字的投票改变,第n个答案的投票不变。我想我需要以某种方式使答案独特,但我不知道如何。我怎样才能解决这个问题?
谢谢 首先要使您的
span
id
唯一。通过执行以下操作
{$allanswers[res].答案{u总费率}
然后在你的电话里
$(".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){
if($.trim(vote_msg)== 'ok')
{
$("#answervotes" + answer_id).text( parseInt($("#answervotes" + answer_id).text()) + 1 );
}
else{
}
}
});
}
)我在这里看到的是,您有多个ID具有相同的名称
应答投票
,因为这是在循环中创建的(如果我没有错的话),所以它正在更新第一个ID
您最好将id更改为类或使其唯一..因为具有相同id的多个元素是无效的,可能会给您带来问题(您现在面临的问题)
尝试此操作(在此处将id更改为类)
{$allanswers[res].答案{u总费率}
还有你的jquery
$(".a_upvote").click(function()
{
var $this=$(this); //<--here
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')
{
$this.siblings(".answervotes").text( parseInt($("#answervotes").text()) + 1 ); //<----here
}
else{
}
}
});
}
)
$(.a_upvote”)。单击(函数()
{
var$this=$(this)//