我似乎无法从$ .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>
答案 0 :(得分:4)
您应该在回调函数之前备份this
:
$(".idea").each(function() {
var $this = $(this),
title = $this.html();
$.post("votes.php", { title: title }, function(data) {
$this.nextAll(".voteTotal").html(data);
}, "json");
});
答案 1 :(得分:0)
使用context
设置,然后就可以了:
$('.idea').each(function(){
var title = $(this).html();
$.post("votes.php", { title: title, context: this }, function(data){
$(this).nextAll('.voteTotal').html(data);
}, "json");
});