javascript - Jqueryeach() 函数与 Ajax 循环

标签 javascript jquery ajax each

我希望用我的 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/

相关文章:

javascript - 将数据发送到另一个 html 文件

javascript - 如何在 React 中为 react-helmet 做 meta 标签和标题单元测试?

javascript - 下拉列表的选定选项值未被拾取 jQuery

javascript - 页面加载后禁用跳转到顶部

javascript - 将下拉列表中选定选项的属性作为参数传递给 'onchange' 中的函数

ajax - 使用 fetch 而不是带有 redux-observable 的 ajax

javascript - 是否可以使用 javascript 更改 http 请求的 header ?

javascript - 将值从 ember 组件发送到父模板

javascript - 如果表单非常大,应该多久保存一次数据

javascript - 如何使用 jQuery 更改媒体查询 CSS