Jquery中Ready函数的多个Ajax查询

时间:2017-11-13 13:36:15

标签: jquery document-ready onload-event

我有一张类似下图的表格 enter image description here

我希望在通过jquery .get method

加载页面后填写“ROL”列中的数据

要做到这一点,我使用下面提到的代码

$(function() {
    setTimeout(function() {
        $('.details').each(function() {
            locid = $(this).find('.locationId').html();
            prodid = $(this).find('.productId').html();
            alert(prodid);
            roqty = $(this).find('.roqty');

            $.get('myUrl', null, function(d) {
                CC = JSON.parse(d);
                roqty.html(CC.roqty);
            });
        });
    }, 5000);
})

加载页面后,只填充ROL列的最后一个单元格。请参见下图。

enter image description here

虽然alert发生了3次。 可能是什么问题?

1 个答案:

答案 0 :(得分:1)

由于您已将roqty定义为全局变量,因此在等待$.get('myUrl')完成时,其引用将更新为最后一个元素。

roqty定义为作为each()回调函数作用的局部变量。

var roqty = $(this).find('.roqty');