1

我似乎无法从 $.post 部分中访问 $(this) 。它在外面工作得很好。这是javascript:

    $('.idea').each(function(){
        var title = $(this).html();
        $.post("votes.php", { title: title }, function(data){
            $(this).nextAll('.voteTotal').html(data);
        }, "json");
    });

HTML:

<h3 class="idea">Idea #1</h3>
<h4 class="voteTotal"></h4>
<p>This is a really cool idea.</p>
<a href="#" class="vote">Click to vote</a>
4

2 回答 2

4

this您应该在回调函数之前备份:

$(".idea").each(function() {
    var $this = $(this),
        title = $this.html();

    $.post("votes.php", { title: title }, function(data) {
        $this.nextAll(".voteTotal").html(data);
    }, "json");
});
于 2012-11-16T12:01:31.863 回答
0

使用context设置,然后它会工作:

$('.idea').each(function(){
    var title = $(this).html();
    $.post("votes.php", { title: title, context: this }, function(data){
        $(this).nextAll('.voteTotal').html(data);
    }, "json");
});
于 2012-11-16T12:11:23.407 回答