我似乎无法从 $.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>
最佳答案
你应该在回调函数之前备份this
:
$(".idea").each(function() {
var $this = $(this),
title = $this.html();
$.post("votes.php", { title: title }, function(data) {
$this.nextAll(".voteTotal").html(data);
}, "json");
});
关于javascript - 你能在 $.post() 中使用 $(this) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13416205/