一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

JS操作input标签属性checkbox全选的实现代码

时间:2017-05-16 编辑:简简单单 来源:一聚教程网

 

 代码如下 复制代码

< >

functionselectAll(){

 varchecklist = document.getElementsByName ("selected");//获取所有name值为selected的标签

 if(document.getElementById("controlAll").checked){//判断当id为controlAll的标签是否被选中

 for(vari=0;i

  checklist[i].checked =true;//当被选中时,则获取所有name值为selected的标签都被选中

 }

 }else{

 for(varj=0;j

  checklist[j].checked =false;//当不被选中时,则获取所有name值为selected的标签都不被选中

 }

 }

}

全选

1:

2:

3:

4:

5:

6:

< type="text/ ">

functionheheda(){

 varcontrolAll = document.getElementById("controlAll");//获取id值为controlAll的标签

 varchecklists = document.body.querySelectorAll("input[type='checkbox']").length;//获取所有格式为checkbox的input标签的数量

 varselectedLength=document.body.querySelectorAll("input[name=selected]:checked").length;//获取所有name值为selected,并且已经被选中的input标签的数量

 if(controlAll.checked){//判断当id为controlAll的标签是否被选中

   controlAll.checked =false;//当被选中时,则自动取消id为controlAll的标签选中

 }else{

  if(selectedLength == checklists-1){//当不被选中时,判断当获取所有name值为selected,并且已经被选中的input标签的数量 与 获取所有格式为checkbox的input标签的数量-1 是否相等

  controlAll.checked =true;//如果相等,则自动选中id为controlAll的标签

  }

 }

 }

 

热门栏目