For循环中的JQuery/Ajax调用 [英] JQuery/Ajax calls in a For Loop

查看:57
本文介绍了For循环中的JQuery/Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将("xxxxx").html(data)放入for循环中,其中"xxxx"变量每次都会更改?

Is it possible to put ("xxxxx").html(data) in a for loop where the "xxxx" variable changes each time?

老实说,我觉得我已经尝试了一切.我正在尝试通过循环ajax调用中的JSONP数据填充HTML表,其中URL每次都会更改.每次都使用相同的回调函数,但是很明显,我一直在覆盖要发送到HTML表格标签的数据,因为我想不出一种动态更改这些变量的方法.

I honestly feel like I've tried everything. I'm trying to fill up an HTML table with JSONP data from a looping ajax call where the URL changes each time. The same callback function is used every time, but, obviously, I keep overwriting the data that is to be sent to the HTML table tags because I can't figure out a way to dynamically change these variables.

基本上,我希望第一次调用回调函数以将数据存储在类似...

Basically, I want the first time the callback function is called to store data in something like...

$("#p1_points").html(data_from_ajax_call)

我第二次想要它做...

The second time I want it to do...

$("#p2_points").html(data_from_ajax_call)

我尝试过一些愚蠢的事情,例如(在for循环内)...

I've tried silly things like (inside a for loop) doing ...

$("#p" + i + "_points").html(data_from_ajax_call)

和各种各样有趣的东西,但是它不接受任何形式的论据...那么,有什么想法吗?这是琐碎的事,我只是想得太多而睡不着呢?

and all sorts of fun stuff, but it doesn't accept arguments of any kind... So, any thoughts? Is this trivial and I'm just over-thinking and under-sleeping it?

在此先感谢您的帮助.

更新清晰度

我的ajax调用看起来像这样...

My ajax calls look like this...

for (var i = 0; i < thisweeksraiders.length; i++){
    $.ajax({
        "url":"http://us.battle.net/api/wow/character/aerie-peak/" + thisweeksraiders[i] + "?jsonp=myCallback",
        "type":"GET",
        "data": { fields: "items, professions, talents, progression"},
        "dataType":"jsonp",
        "contentType":"application/json",
        "jsonpCallback":"myCallback",
        "success":function(data1){
        }
    })
}

我的回调函数看起来像这样...

and my callback function looks like this...

function myCallback(data1) {
        //itemscounter += 1;
        var hascloak = "No";
        var possupgrades = 30;
        var offhandequipped = false;
        var tempupgrades = 0;
        var tierequipped = 0;
        for (var i = 0; i < gearlist.length; i++){
            if (data1.items[(gearlist[i])].tooltipParams.upgrade)
                tempupgrades += data1.items[(gearlist[i])].tooltipParams.upgrade.current;
        }
        for (var i = 0; i < tierlist.length; i++){
            if(data1.items[(tierlist[i])].tooltipParams.set)
                tierequipped += 1;
        }
        if (data1.items.offHand){
            tempupgrades += data1.items.offHand.tooltipParams.upgrade.current;
            offhandequipped = true;
        }
        if (offhandequipped)
            possupgrades = 32;
        if(data1.items[(gearlist[3])].quality == 5)
            hascloak = "Yes";
        $("#p1_cloak").html(hascloak);
        $("#p1_tier").html(tierequipped + "/5");
        $("#p1_achieve").html(data1.achievementPoints);
        $("#p1_iLevelE").html(data1.items.averageItemLevelEquipped);
        $("#p1_upgrades").html(tempupgrades + "/" + possupgrades);
        var totalnormalkills = 0;
        var totalheroickills = 0;
        var furthestboss = null;
        for (var i = 0; i < soobosslist.length; i++){
            totalnormalkills += data1.progression.raids[31].bosses[i].normalKills;
            totalheroickills += data1.progression.raids[31].bosses[i].heroicKills;
        }
        if (totalheroickills == 0){
            for (var i = 14; i > 0; i--){
                if (data1.progression.raids[31].bosses[i-1].normalKills > 0){
                    furthestboss = i + "N";
                    break;
                }
            }
        }
        else {
            for (var i = 14; i > 0; i--){
                if (data1.progression.raids[31].bosses[i-1].heroicKills > 0){
                    furthestboss = i + "H";
                    break;
                }
            }
        }
        $("#p1_normalkills").html(totalnormalkills);
        $("#p1_heroickills").html(totalheroickills);
        $("#p1_xp").html(furthestboss);
        var classtemp;
        var colortemp;
        switch(data1.class){
        case 1: classtemp = "Warrior"; colortemp = "#C79C6E"; break;
        case 2: classtemp = "Paladin"; colortemp = "#F58CBA"; break;
        case 3: classtemp = "Hunter"; colortemp = "#ABD473"; break;
        case 4: classtemp = "Rogue"; colortemp = "#FFF569"; break;
        case 5: classtemp = "Priest"; colortemp = "#FFFFFF"; break;
        case 6: classtemp = "Death Knight"; colortemp = "#C41F3B"; break;
        case 7: classtemp = "Shaman"; colortemp = "#0070DE"; break;
        case 8: classtemp = "Mage"; colortemp = "#69CCF0"; break;
        case 9: classtemp = "Warlock"; colortemp = "#9482C9"; break;
        case 10: classtemp = "Monk"; colortemp = "#00FF96"; break;
        case 11: classtemp = "Druid"; colortemp = "#FF7D0A"; break;
        }
        $("#p1_classspec").html("<font color=" + colortemp + "><b>" + data1.name + "</b></font><font size='-1' color=" + colortemp + "><em> (" + data1.talents[0].spec.name + ")</em></font>");
        var profstemp = (data1.professions.primary[0].name + " & " + data1.professions.primary[1].name);
        $("#p1_profs").html(profstemp);
    }

因此,基本上,我可以在函数末尾放置的所有#p1内容,但我想将其全部更改为$ p2,以填充表的下一行.HTML应该很明显,但是在这里...

So, basically, all the #p1 stuff I could put at the end of the function, but I'd like to change it all to $p2 to fill in the next row on my table. The HTML should be obvious, but here it is...

<body>
<center>
<table width="100%" border="0">
  <tr>
    <td id="p1_classspec">&nbsp;</td>
    <td id="p1_iLevelE">&nbsp;</td>
    <td id="p1_upgrades">&nbsp;</td>
    <td id="p1_cloak">&nbsp;</td>
    <td id="p1_tier">&nbsp;</td>
    <td id="p1_profs">&nbsp;</td>
    <td id="p1_achieve">&nbsp;</td>
    <td id="p1_normalkills">&nbsp;</td>
    <td id="p1_heroickills">&nbsp;</td>
    <td id="p1_xp">&nbsp;</td>
  </tr>
  <tr>
    <td id="p2_classspec">&nbsp;</td>
    <td id="p2_iLevelE">&nbsp;</td>
    <td id="p2_upgrades">&nbsp;</td>
    <td id="p2_cloak">&nbsp;</td>
    <td id="p2_tier">&nbsp;</td>
    <td id="p2_profs">&nbsp;</td>
    <td id="p2_achieve">&nbsp;</td>
    <td id="p2_normalkills">&nbsp;</td>
    <td id="p2_heroickills">&nbsp;</td>
    <td id="p2_xp">&nbsp;</td>
  </tr>
  <tr>
    <td id="p3_classspec">&nbsp;</td>
    <td id="p3_iLevelE">&nbsp;</td>
    <td id="p3_upgrades">&nbsp;</td>
    <td id="p3_cloak">&nbsp;</td>
    <td id="p3_tier">&nbsp;</td>
    <td id="p3_profs">&nbsp;</td>
    <td id="p3_achieve">&nbsp;</td>
    <td id="p3_normalkills">&nbsp;</td>
    <td id="p3_heroickills">&nbsp;</td>
    <td id="p3_xp">&nbsp;</td>
  </tr>
</table>
</center>
</body>

如果您单击此链接,您将看到我要做什么(这没有使用for循环).我只想大幅度减少代码.

If you follow this link, you'll see what I'm going for (this is not using the for loop). I just want to drastically cut down on my code.

http://www.mynextbit.com/Pages/Wreckedified/test2.html

Patrick的另一个更新...

One more Update for Patrick...

$(document).ready(function(){
        for (var i = 0; i < thisweeksraiders.length; i++){
            (function(index)
                window.jsonpCallbacks["myCallback" + index] = function(data){
                    myCallback(data,index);
                };
        })(i);
            $.ajax({
                "url":"http://us.battle.net/api/wow/character/aerie-peak/" + thisweeksraiders[i] + "?jsonp=jsonpCallbacks.myCallback" + i,
                "type":"GET",
                "data": { fields: "items, professions, talents, progression"},
                "dataType":"jsonp",
                "contentType":"application/json",
                "jsonpCallback":"jsonpCallbacks.myCallback"+i,
                "success":function(data1){
                }
            })
        }

    });

推荐答案

如果循环,则如下所示:

if you loop looks something like this:

for(var i=0; i<10; i++){
   $.ajax({
    //
    success:function(data){
       $("#p" + i + "_points").html(data);
    }
   });
}

它不起作用,因为 i 最终将成为循环中的最后一个 i 值;您需要类似

it will not work as i will end up being the last i value in the loop; You need something like below

for(var i=0; i<10; i++){
   (function(index){
      $.ajax({
       //
       success:function(data){
          $("#p" + index + "_points").html(data);
       }
      });
   })(i);
}

闭包以及 i 的传递将保留该调用的数字值.

The closure along with the passing of i will keep number value for that call.

当然,将需要存在ID为1-10或您使用的任何数字的元素:

of course there will need to exist elements with ids 1-10 or whatever number you use so:

<element id="p1_points">
<element id="p2_points">
<element id="p3_points">
...

编辑以说明JSONP回调:

EDIT to account for JSONP callback:

将myCallback()修改为:

modify myCallback() to be:

function myCallback(data,index)

并使用索引使您的#p" + index +"_ points" 等ID

and use the index to make your "#p"+index+"_points" etc ids

然后是循环和ajax:

and then for the loop and ajax:

//Keeps track of the callbacks
//we will prepend 'jsonpCallbacks.' to the callback names
window.jsonpCallbacks = {};

for (var i = 0; i < thisweeksraiders.length; i++){
    (function(index){
      window.jsonpCallbacks["myCallback"+index] = function(data){
         myCallback(data,index);
      };
    })(i);
    $.ajax({
        "url":"http://us.battle.net/api/wow/character/aerie-peak/" + thisweeksraiders[i] + "?jsonp=jsonpCallbacks.myCallback"+i,
        "type":"GET",
        "data": { fields: "items, professions, talents, progression"},
        "dataType":"jsonp",
        "contentType":"application/json",
        "jsonpCallback":"jsonpCallbacks.myCallback"+i,
        "success":function(data1){
        }
    })
}

这篇关于For循环中的JQuery/Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆