所以我有这个HTML:
<td class="score_section">
<div class="row collapse" align="center">
<div class="upvotebutton" onclick="javascript: Upvote()">▲</div>
<div class="votescore"> 40 </div>
<div class="donwvotebutton" onclick="javascript: Downvote()">▼</div>
</div>
</td>
这是产品表的一部分,所以更多的行有这个类“votescore”,我已经为downvote / upvote创建了一个jQuery(ajax)函数:
function Upvote(user_id, thread_id) {
var vote = +1;
$.ajax({
type: "post",
url: "Ajax/vote-o-matic.php",
data: {user_id: user_id, thread_id: thread_id, vote: vote},
success: function(res) {
jquery(this).$('.votescore').html(res);
}
});
}
成功完成downvote或upvote刷新特定的“votingcore”div(与upvote / downvote按钮相对应的div)与来自ajax的传入数据。
一切正常,只有当我点击它刷新所有“votescore”div,任何想法修复或解决这个问题?
答案 0 :(得分:2)
您可以绑定侦听器,然后允许您使用this
$('.upvotebutton').on('click', function(){
var me = $(this);
var vote = +1;
$.ajax({
type: "post",
url: "Ajax/vote-o-matic.php",
data: {
user_id: user_id,
thread_id: thread_id,
vote: vote
},
success: function (res) {
me.parent().find('.votescore').html(res);
}
});
});
答案 1 :(得分:-1)
你应该给它一个ID,然后用它来刷新div。
<td class="score_section" id="section1">
<div class="row collapse" align="center">
<div class="upvotebutton" onclick="javascript: Upvote(section1)">▲</div>
<div class="votescore"> 40 </div>
<div class="donwvotebutton" onclick="javascript: Downvote(section1)">▼</div>
</div>
</td>
然后:
function Upvote(div_id,user_id, thread_id){
var vote = +1;
$.ajax({
type: "post",
..
success: function(res){
$(div_id).html(res);
}
});
}