【问题标题】:Pass response of AJAX only to current div among an array仅将 AJAX 的响应传递给数组中的当前 div
【发布时间】: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 的类以外的其他东西,例如 &lt;span class='score'&gt;,并让维护它的人更轻松。
  • @Donalda 我已经回答了,您需要获取用户单击的锚标记的父级,然后在该父级 div 中找到您想要的类。

标签: javascript jquery html arrays ajax


【解决方案1】:

你不能在成功函数中使用 $(this),因为它会随着调用 $(this) 的上下文而改变,你必须将它存储在一个 var 中,然后在你的 ajax 成功中使用它,如下所示:

<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({ type: "POST", url: "up_vote.php", data: dataString, cache: false, success: function (html){ $(this).parent().find('.this').html(html); }}); }
  • 这是我的 .this 类的 html ----
  • @DonaldaDonalda 尝试使用 firefox firebug 控制台,然后使用 console.log($(this).parent() );它应该打印 "
    " 如果没有然后让我知道它打印的内容,当您将鼠标悬停在控制台中的打印文本上时,您可以看到它在浏览器中指向的位置,因为它将突出显示元素你的页面。
  • 我应该在我的 php 文件或 firebug 控制台中的某处写吗?
  • 在你的php文件中像这样:成功:function(html){console.log($(this).parent()); //遍历到单击的锚标记的容器,然后在该容器中找到您的“.this”类并更改其html $(this).parent().find('.this').html(html); }
【解决方案2】:

您尚未显示代码,但大概是在单击 .vote 元素时触发 AJAX 请求。如果是这种情况,您可以存储对引发事件的元素的引用,然后使用 DOM 遍历找到兄弟.this 并将其更改为 HTML。像这样的:

$('.vote').click(function(e) {
    e.preventDefault();
    var $vote = $(this); // store the element which raised the event here
    $.ajax({
        url: 'foo.php',
        success: function(html) {
            $vote.siblings('.this').html(html); // use siblings() to find .this to change
        }
    });
});

【讨论】:

  • 这里是代码 - 它不起作用 $(".vote").click(function() { var $vote = $(this); var id = $(this).attr(" id"); var name = $(this).attr("name"); var dataString = 'id='+ id ; var acdc = $('#ab').attr('class'); var potato = '土豆'; var parent = $(this); if(name=='up') { $.ajax({ type: "POST", url: "up_vote.php", data: dataString, cache: false, 成功: function(html){ $vote.siblings('.this').html(html); }}); } else
  • 您是否在控制台中检查以确保 AJAX 请求正常工作?
猜你喜欢
相关资源
最近更新 更多
热门标签