在 Jquery asp.net 中迭代嵌套循环
我正在处理一个页面,其中我有 2 个嵌套循环,我的问题是...举例来说,外循环的当前值为 1 并且应执行 1 次。内循环执行2次。现在,当我警告内循环中外循环的值时,第一次尝试时它会抛出 2。我不明白它是如何得到 2 的,因为我的初始值为 1 并且条件 <=1。当我尝试打印内部循环的值时,它第一次返回 1,第二次返回 0...这是疯狂的行为...我完全被困在这里...下面是我的代码。
for (i = 1; i <= _leftSectionCount; i++) //_leftSectionCount is 1{
$("#tdSection" + i + "Image").html("<img width='500' height='35' src='/newsletterimage/section" + i + ".gif'>");
var rows = $("#divLeft" + i + "tbody1 td:nth-child(1)"); // has 2 rows
if (rows.length > 0) {
$("#tdSection" + i + "Data").append("<table id='tblSection" + i + "Data'></table>");
$("#tblSection" + i + "Data").html("");
}
rows.each(function (index) {
$.ajax({
type: "POST",
url: "AjaxMethods.aspx/GetArticleDetails",
data: "{'articleId':'" + $(this).text() + "'}",
dataType: "json",
contentType: "application/json",
success: function (data) {
var results = data.d;
alert("$(#tblSection" + i + "Data')"); // this returns me 2.
alert("$(#tblSection" + index + "Data')"); // this returns me 1 and 0.
$("#tblSection" + i + "Data").append("<tr><td>" + results[0].Title + "</td></tr>");
},
error: function (data) { alert(data.responseText); }
});
})
WriteToFile();
}
任何人请帮助我,我完全被困在这里。
i am working on a page wherein i have 2 nested loops, my problem is... Say for Example the current value for outer loop is 1 and shall execute 1times. and inner loop shall execute 2times. now when i alerts the value of outer loop in the inner loop, it throws me 2, on first attempt.. i dont understand how it gets 2, as my intial value is 1 and condition is <=1. and when i try to print the value of inner loop, it returns 1 on first and 0 second time... this is crazy behaviour... i am totally stuck here... below is my code.
for (i = 1; i <= _leftSectionCount; i++) //_leftSectionCount is 1{
$("#tdSection" + i + "Image").html("<img width='500' height='35' src='/newsletterimage/section" + i + ".gif'>");
var rows = $("#divLeft" + i + "tbody1 td:nth-child(1)"); // has 2 rows
if (rows.length > 0) {
$("#tdSection" + i + "Data").append("<table id='tblSection" + i + "Data'></table>");
$("#tblSection" + i + "Data").html("");
}
rows.each(function (index) {
$.ajax({
type: "POST",
url: "AjaxMethods.aspx/GetArticleDetails",
data: "{'articleId':'" + $(this).text() + "'}",
dataType: "json",
contentType: "application/json",
success: function (data) {
var results = data.d;
alert("$(#tblSection" + i + "Data')"); // this returns me 2.
alert("$(#tblSection" + index + "Data')"); // this returns me 1 and 0.
$("#tblSection" + i + "Data").append("<tr><td>" + results[0].Title + "</td></tr>");
},
error: function (data) { alert(data.responseText); }
});
})
WriteToFile();
}
Anyone please help me i am totally stuck here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的响应将异步返回,因此
i
将是任何内容,当它返回时,更改为async=false
以保留i
和 <代码>索引同步(无双关语):Your responses are coming back async, so
i
will be whatever it is, when it returns, change toasync=false
to keepi
andindex
in sync(no pun):