开发者

for loop to check if a box is selected

i have the following:

for(var i:Number = 1; i <= 10; i++) {
if ( eval("chk"+i).selected == false ) {
    btnSubmit.enabled = false;
开发者_如何学Python    } else {
    btnSubmit.enabled = true;
   }
}

i have 10 boxes. If anyone of them is checked, then i enable the form submit button. for some reason, the above code works for only the 10th item. any ideas why?


You are looping through each checkbox in turn, and setting the submit button to be enabled if that checkbox is ticked, and disabled if it is not.

This means that the last checkbox is the only one that counts, since every checkbox undoes whatever the previous one did.

Try this instead:

btnSubmit.enabled = false;
for(var i:Number = 1; i <= 10; i++) {
   if ( eval("chk"+i).selected == true ) {
      btnSubmit.enabled = true;
   }
}

Now you set the button to be disabled, and then only set it to be enabled if any checkbox is ticked.


add a break statement after the btnSubmit.enabled = true to stop looping immediately. small thing in this case.... but...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜