我希望用我的 ajax 结果替换每个 div 类中的值,但我似乎无法在循环期间将我的表结果附加到各个 div 类。我知道 $(this).append(table)
放置错误,因为它不在 ajax 请求之外。我该如何修改它以获得我想要的效果?
我的脚本是这样的:
$('.developer_badgesarea').each(function(){
// get the div class value to perform ajax
var player_id = $(this).html();
var table;
// if condition to conduct ajax
if(player_id != 'None'){
$.ajax({
// ajax stuff here
success: function(result){
//table created here
$(this).append(table);
}
});
}
});
最佳答案
问题是你在ajax成功函数中的“this”引用。它引用回调函数,而不是您想要引用它的 dom 元素。
$('.developer_badgesarea').each(function(){
// element reference to your div, that you'll access inside your ajax call
var elm = $(this);
// get the div class value to perform ajax
var player_id = elm.html();
var table;<p></p>
<pre><code>// if condition to conduct ajax
if(player_id != 'None'){
$.ajax({
// ajax stuff here
success: function(result){
//table created here
elm.append(table);
}
});
}
</code></pre>
<p>});
</p>
关于javascript - Jqueryeach() 函数与 Ajax 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5547702/