在 Jquery asp.net 中迭代嵌套循环

发布于 2024-12-02 16:34:52 字数 1849 浏览 0 评论 0原文

我正在处理一个页面,其中我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱暴击 2024-12-09 16:34:52

您的响应将异步返回,因此 i 将是任何内容,当它返回时,更改为 async=false 以保留 i 和 <代码>索引同步(无双关语):

rows.each(function (index) {
    $.ajax({
    type: "POST",
    url: "AjaxMethods.aspx/GetArticleDetails",
    data: "{'articleId':'" + $(this).text() + "'}",
    dataType: "json",
    async: false, //HERE
    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); }
    });
})

Your responses are coming back async, so i will be whatever it is, when it returns, change to async=false to keep i and index in sync(no pun):

rows.each(function (index) {
    $.ajax({
    type: "POST",
    url: "AjaxMethods.aspx/GetArticleDetails",
    data: "{'articleId':'" + $(this).text() + "'}",
    dataType: "json",
    async: false, //HERE
    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); }
    });
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文