每个循环中的ajax执行不正确

tra*_*ong 3 ajax each jquery

我有以下js片段:

cat_images = $(".category-description").next().find("img");
cat_images.each(function () {
    url = $(this).parent().attr("href");
    id = url.split("id=");
    id = id[1];
    url = "cat_url.php?i="+id;
    this_image = this;

    $.get (url, function (data) {
        $(this_image).attr("src", data);
    });
 });
Run Code Online (Sandbox Code Playgroud)

目前,ajax.get部分仅更新每个循环迭代的最后一个元素.我认为这与ajax与循环交互不良有关,因为this在ajax函数中使用会使其完全失败.

无论如何,在等待ajax在Jquery中完成时,有没有延迟循环?(宁愿不混合普通的js)

Sel*_*gam 6

尝试将其包裹在封口内,

cat_images.each(function () {
    url = $(this).parent().attr("href");
    id = url.split("id=");
    id = id[1];
    url = "cat_url.php?i="+id;
    this_image = this;

    (function (this_image) {
      $.get (url, function (data) {
        $(this_image).attr("src", data);
      });
    })(this_image);
 });
Run Code Online (Sandbox Code Playgroud)