问题描述
我已经成功地进行简单的投票系统单次成绩。一切都很好,但问题是,当我点击一个按钮进行投票这个特定内容 - 在其他div的所有得分随之改变!我需要AJAX输出成功的答案只对当前格(视频votting)不是所有的人在我的网页!我认为它是因为这样的:
I've managed to make simple voting system with single current score. Everything is fine but the problem is that when I click on a button to vote for this particular content - all scores in other divs change as well!! I need AJAX to output the answer on success only on current div (video votting) not all of them on my page! I think its because of this:
success: function(html) {
$('.this').html(html);
}
这是因为我有块阵列我的网页上:
That's because I have an array of blocks on my page:
<div id="main">
<div class="box1">
<a href="" class="vote" id="<?php echo $mes_id; ?>" name="up">
<img class='image'src="img/thumbsup.png">
</a>
<span class='this'><?php echo $total_score; ?></span>
<a href="" class="vote" id="<?php echo $mes_id; ?>" name="down">
<img class='image' src="img/icon-down.png">
</a>
</div>
<div class='box2' ><?php echo $msg; ?></div>
</div>
和带班'这个',当成功的AJAX增加了针对所有的人,但我需要找出他们只有到当前添加响应(带班跨越这个,这基本上是目前的分数跨度投票),这样,当用户投票 - 只有当前得分的变化。不是所有的人! (如果个个都带班跨度'这个',而我通过响应从AJAX制作类特定的每个块(DIV)是废话,因为我做一个网页,显示来自数据库的视频 - 任何数量!
这里就是Ajax和jQuery:
And span with class 'this', when on success AJAX adds response to all of them, but I need to identify them and add response only to current one (spans with class 'this', which are basically the scores of current voting), so that when user votes - only current score changes. not all of them! (if all of them have spans with class 'this' to which I pass response from AJAX. Making a specific class for each block(div) is nonsense, because I make a page that displays videos from database - any number!here is ajax and jquery:
<script type="text/javascript">
$(function() {
$(".vote").click(function()
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var acdc = $('#ab').attr('class');
var potato = 'potato';
var parent = $(this);
if(name=='up')
{
$.ajax({
type: "POST",
url: "up_vote.php",
data: dataString,
cache: false,
success: function(html){
$('.this').html(html);
}});
}
else
{
$(this).fadeIn(200).html('<img src="img/icon-down.png" align="absmiddle" style="height: 10px;width:10px;">');
$.ajax({
type: "POST",
url: "down_vote.php",
data: dataString,
cache: false,
success: function(html){
$('.this').html(html);
}});
}
return false;
});
});
</script>
所以,我怎么能呢?(只改变当前分)非常感谢!在前进。
So, how can I make that?(change only current score) Thanks a lot! In advance.
推荐答案
您不能成功,函数内部,因为它会随着环境的变化使用$(本),其中$(本)被调用时,你必须保存它在VAR,然后使用它像下面在阿贾克斯的成功:
You can't use $(this) inside success function as it will changes with context in which $(this) is called, you have to store the it in a var and then use it like below in your ajax success:
<script type="text/javascript">
$(function () {
$(".vote").click(function ()
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id=' + id;
var acdc = $('#ab').attr('class');
var potato = 'potato';
var parent = $(this);
if (name == 'up')
{
$.ajax({
type: "POST",
url: "up_vote.php",
data: dataString,
cache: false,
success: function (html) {
parent.parent().find('.this').html(html);
}});
}
else
{
$(this).fadeIn(200).html('<img src="img/icon-down.png" align="absmiddle" style="height: 10px;width:10px;">');
$.ajax({
type: "POST",
url: "down_vote.php",
data: dataString,
cache: false,
success: function (html) {
parent.parent().find('.this').html(html);
}});
}
return false;
});
});
</script>
这篇关于通过AJAX的回应只是当前格数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!