jquery异步循环获取功能实现代码

  html代码:

  

复制代码 代码如下:

  <table id="repTable"……>

  ……

  <span id="<%# Eval("Id") %>" class="address"></span>

  </table>

  jquery代码:

  

复制代码 代码如下:

  $(document).ready(function(){

  $("#repTable span.address").each(function(){

  var spanTemp = $(this);

  $.ajax({

  type: "get",

  url: "SceneryAjaxCall.aspx",

  data: "sid="+$(this).attr("id"),

  cache: true,

  dataType: "html",

  success: function(msg){

  spanTemp.append(msg);

  }

  });

  });

  });

  其中在写jquery代码时,我遇到了下面几个问题:

  (1)开始没写第三行,而是在第十一行直接调用的$(this),报错“不能给回调函数赋值”。原因是回调后对象已经发生改变,所以需要事先保存;

  (2)没写dataType,发现不能返回msg,不知道为什么;