Select All Checkboxes By ID/Class

后端 未结 8 1880
孤城傲影
孤城傲影 2021-02-07 12:27

I\'m really bad at Javascript and I\'m struggling to get my head round it.

What I\'m trying to do is get something to select all checkboxes. However everything I have fo

8条回答
  •  离开以前
    2021-02-07 12:52

    var array = document.getElementsByTagName("input");
    
    
    for(var ii = 0; ii < array.length; ii++)
    {
    
       if(array[ii].type == "checkbox")
       {
          if(array[ii].className == YOUR_CLASS_NAME)
           {
            array[ii].checked = true;
    
           }
    
    
       }
    }
    

    by ID:

    var checkbox = document.getElementById(YOUR_ID);
    
    if(null != checkbox)
        checkbox.checked = true;
    

提交回复
热议问题