简体   繁体   English

使用for循环在document.getelementbyid中填充数组

[英]Using for loop to populate array in document.getelementbyid

i now facing a problem with using looping on my project. 我现在在项目上使用循环时遇到问题。 Currently i have a multiple checkbox which have their own unique id example :myCheckbox,myCheckBox1,myCheckBox2,.... 目前,我有多个复选框,它们都有自己的唯一ID示例:myCheckbox,myCheckBox1,myCheckBox2,....

I don't wish to hardcore the number behind the myCheckBox, so i try to use a for loop to get it done. 我不希望对myCheckBox背后的数字加硬,因此我尝试使用for循环将其完成。 Somehow i dono why my for loop does work, if i hardcode it like document.getelementbyid('mycheckbox1').checked==true then it work. 我以某种方式不知道为什么我的for循环确实起作用,如果我像document.getelementbyid('mycheckbox1')。checked == true这样对其进行硬编码,那么它就可以工作。

Anything i miss out in my code? 我在代码中错过了什么吗? Please guide me through as i'm still new to web programming. 请指导我,因为我还是Web编程的新手。 Thanks 谢谢

Here is my code: 这是我的代码:

<script type='text/javascript'>

        function checkDisabled(yourSubmitButton){

          for(var i=0;i<10;i++){
              if(document.getElementById("myCheckBox"+i).checked==true){
                  yourSubmitButton.disabled = false;
                    return; 
              }
          } 


            // If we have made it here, disable it
            yourSubmitButton.disabled = true;

          };
          </script> 

在黑暗中myCheckbox :您在问题中说过,您的复选框称为myCheckboxmyCheckbox1myCheckBox2等,但是您的循环在第一次迭代时正在寻找myCheckbox0 ,而不是myCheckbox ,这将使其在发生异常时抛出异常您尝试读取nullchecked属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM