【发布时间】:2016-07-21 09:12:40
【问题描述】:
我已经设法制作了具有单一当前分数的简单投票系统。一切都很好,但问题是当我点击一个按钮来为这个特定的内容投票时 - 其他 div 中的所有分数也会改变!!我需要 AJAX 仅在当前 div(视频投票)上输出成功的答案,而不是在我的页面上全部输出!我认为是因为这个:
success: function(html) {
$('.this').html(html);
}
那是因为我的页面上有一组块:
<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>
并且跨越类'this',当成功时AJAX会添加对所有这些的响应,但我需要识别它们并只添加对当前的响应(跨越类'this',这基本上是当前的分数投票),因此当用户投票时 - 只有当前分数发生变化。不是所有的人! (如果它们都具有类“this”的跨度,我将 AJAX 的响应传递给它。为每个块(div)创建一个特定的类是无稽之谈,因为我创建了一个显示来自数据库的视频的页面 - 任意数量! 这是ajax和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>
那么,我怎样才能做到这一点?(仅更改当前分数)非常感谢!提前。
【问题讨论】:
-
我建议您使用除名为
this的类以外的其他东西,例如<span class='score'>,并让维护它的人更轻松。 -
@Donalda 我已经回答了,您需要获取用户单击的锚标记的父级,然后在该父级 div 中找到您想要的类。
标签: javascript jquery html arrays ajax