【问题标题】:AJAX/Jquery to update only a particular div on clicking particular buttonAJAX/Jquery 在单击特定按钮时仅更新特定 div
【发布时间】:2011-07-30 09:24:38
【问题描述】:

我有以下 AJAX 函数来更新点击的投票。

$(function() {
            $(".vote").click(function()
            {
            var id = $(this).attr("id");
            var name = $(this).attr("name");
            var dataString = 'id='+ id ;
            var parent = $(this);

            if (name=='up')
            {
            $(this).fadeIn(200).html('<img src="/content/voting/yes-enb.JPG" />');
            $.ajax({
            type: "POST",
            url: "up_vote.php",
            data: dataString,
            cache: false,

            success: function(html)
            {
            parent.html(html);
            }
            });
            }

});

div/span html 代码如下

<span class="vote"  id="53" name="up" style="text-decoration:none; color:none; margin:5px; ">
<img src="/content/voting/yes-enb.png" width=12 height=12 alt=""> 
<span style="text-decoration:none; color:none">'.$up.' </span></span>

所以上面的 AJAX 代码在用户每次点击投票按钮时递增计数器。但是在这种情况下,投票 img 以及投票计数都会被刷新。我想要的只是'.$up'。部分得到刷新。那只是投票部分。

知道如何做到这一点吗?

【问题讨论】:

    标签: php jquery ajax


    【解决方案1】:

    您应该使用请求中的数据仅使用“投票”类更新跨度内的跨度。

    所以改变

    parent.html(html);
    

    parent.find('span').html(html);
    

    确保你的 php 文件只返回投票值以避免重复箭头:)

    【讨论】:

      【解决方案2】:

      使用$('.vote &gt; span').html(html) 而不是parent.html(html) 并且您的up_vote.php 只需要返回相关号码。

      【讨论】:

        猜你喜欢
        • 2014-09-15
        • 1970-01-01
        • 2013-02-11
        • 1970-01-01
        • 2012-01-19
        • 1970-01-01
        • 1970-01-01
        • 2013-06-30
        • 1970-01-01
        相关资源
        最近更新 更多