嵌套for循环不适用于javascript。 [英] Nested for Loop not working for javascript.

查看:122
本文介绍了嵌套for循环不适用于javascript。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function SelectAccess(CheckBox) {

       var chkAc1 = document.getElementById('ctl00_MainContent_CheckBoxAccess').checked;
            if (document.getElementById('ctl00_MainContent_CheckBoxAccess').checked) {

                var ParentGrid = document.getElementById("<%= gvParentGrid.ClientID %>");

                if (ParentGrid.rows.length > 0) {
                    alert(ParentGrid.rows.length);
                    var chidgrid = $('#<%= gvParentGrid.ClientID %> table');

                    var chidgridVal = document.getElementById('ctl00_MainContent_gvParentGrid_ctl02_gvChildGrid');

                    if (chidgridVal.rows.length > 0) {
                        alert(chidgridVal.rows.length);


                        for (var j = 1; j < 11; j++) {

                            for (var i = 1; i <= chidgridVal.rows.length; i++) {

                               document.getElementById('ctl00_MainContent_gvParentGrid_ctl0' + (j + 1).toString() + '_gvChildGrid_ctl0' + (i + 1).toString() + '_ui_access').checked = true;

                               document.getElementById('ctl00_MainContent_gvParentGrid_ctl0' + (j + 2).toString() + '_gvChildGrid_ctl0' + (i + 1).toString() + '_ui_access').checked = true;
                            }
                       }
                    }
                 }
       }

   }



上面的代码为javascript但是当内部for循环被执行时,外部for不执行而j不会增加,所以for循环不会完全执行。如何解决这个问题?我的外部for循环执行第一次然后内部为循环执行直到行的长度,在执行内部循环之后我的j应该增加并且外循环必须再次得到1,然后最终内循环。

但是我的内部曾被执行过不要去外面并且,例如只有当j = 1的值时,i递增并且执行j的值不会被加入。


The above code for javascript but when the inside for loop gets executed the outer for do not get executed and j do not get incremented so for loop do not get executed completely.How to resolve this?My outer for loop executes at 1st time then inner for loop get executed till the lenght of rows, after execution of innner loop my j should get incremented and outer loop must get exceuted 1's again and then finally inner loop.
But my inner once executed do no go outside and exceution stops so only when value of j=1 the i increments and executed j 's value do not get incrmented.

推荐答案

' #<%= gvParentGrid.ClientID%> table');

var chidgridVal = document.getElementById(' ctl00_MainContent_gvParentGrid_ctl02_gvChildGrid');

if (chidgridVal.rows.length > 0 ){
alert(chidgridVal.rows.length);


for var j = 1 ; j < 11 ; j ++){

for var i = 1 ; i < = chidgridVal.rows.length; i ++){

document.getElementById(' ctl00_MainContent_gvParentGrid_ctl0' +(j + 1 )。toString()+ < span class =code-string>'
_ gvChildGrid_ctl0' +(i + 1 ) .toString()+ ' _ ui_access')。 checked = true ;

document.getElementById(' ctl00_MainContent_gvParentGrid_ctl0' +(j + 2 )。toString()+ ' _ gvChildGrid_ctl0' + (i + 1 )。toString()+ ' _ ui_access ')。已检查 = true ;
}
}
}
}
}

}
('#<%= gvParentGrid.ClientID %> table'); var chidgridVal = document.getElementById('ctl00_MainContent_gvParentGrid_ctl02_gvChildGrid'); if (chidgridVal.rows.length > 0) { alert(chidgridVal.rows.length); for (var j = 1; j < 11; j++) { for (var i = 1; i <= chidgridVal.rows.length; i++) { document.getElementById('ctl00_MainContent_gvParentGrid_ctl0' + (j + 1).toString() + '_gvChildGrid_ctl0' + (i + 1).toString() + '_ui_access').checked = true; document.getElementById('ctl00_MainContent_gvParentGrid_ctl0' + (j + 2).toString() + '_gvChildGrid_ctl0' + (i + 1).toString() + '_ui_access').checked = true; } } } } } }



上面的代码为javascript但是当内部for循环被执行时,外部for不执行而j不会增加,所以for循环不会完全执行。如何解决这个问题?我的外部for循环执行第一次然后内部为循环执行直到行的长度,在执行内部循环之后我的j应该增加并且外循环必须再次得到1,然后最终内循环。

但是我的内部曾被执行过不要去外面并且,例如只有当j = 1的值时,i递增并且执行j的值不会被加入。


The above code for javascript but when the inside for loop gets executed the outer for do not get executed and j do not get incremented so for loop do not get executed completely.How to resolve this?My outer for loop executes at 1st time then inner for loop get executed till the lenght of rows, after execution of innner loop my j should get incremented and outer loop must get exceuted 1's again and then finally inner loop.
But my inner once executed do no go outside and exceution stops so only when value of j=1 the i increments and executed j 's value do not get incrmented.


会员9410081570写道:
Member 9410081570 wrote:

...当内部for循环被执行时,外部为不执行...

…when the inside for loop gets executed the outer for do not get executed…

请原谅我这个表达式,但这是ca lled gibberish http://en.wikipedia.org/wiki/Gibberish [ ^ ]。我会解释......



我希望你搞砸了你的措辞,因为逻辑太简单了。如果内循环正在执行,它的外循环可能如何执行? 如果是这种情况,则执行甚至不会到达内循环。您的观察结果是不正确的:绝对清楚您的外循环将执行10次,但前提是没有抛出异常内循环。如果抛出异常,它仍会执行一次。



现在,关于异常:你的循环从1开始,而不是0,这很奇怪。所有索引都是集合总是从零开始。并且您还将 id 的表达式递增1和2.您确定您的ID从2开始编号吗?也许你需要以常规的零基础重新编号你的ID,以避免混乱。



毕竟,你需要在调试器下运行你的代码,看看到底发生了什么。



-SA

Please forgive me this expression, but this is called gibberish: http://en.wikipedia.org/wiki/Gibberish[^]. I'll explain…

I hope you just messed up your wording, because the logic is way too simple. If inner loop is executing, how its outer loop could probably no executed? If it was the case, the execution would not even reach the inner loop. Your observations are simply incorrect: it's absolutely clear that your outer loop would execute 10 times, but only if there is no exception thrown in an inner loop. If exception is thrown, it will still execute once.

Now, about the exception: it's very strange that your loop starts from 1, not 0. All indexing of all collections is always zero-based. And you also increment the expression for id by 1 and 2. Are you sure that your IDs are numbered starting from 2? Perhaps you need to renumber your IDs in a regular zero-based manner, to avoid mess-up.

After all, you need to run your code under the debugger, to see what's really going on.

—SA


这篇关于嵌套for循环不适用于javascript。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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