JS全选checkBox最佳方案。

 <input type="button" name="" value="不计算长度" onclick="a()">
 
<input type="button" name="" value="全选" onclick="b()">
 
<input type="button" value="缓存"  onclick="c()">
  
<input type="checkbox" name="test[]" value="0"> 测试测试<br>

 

代码
   
     
< input type = " button " name = "" value = " 不计算长度 " onclick = " a() " >
< input type = " button " name = "" value = " 全选 " onclick = " b() " >
< input type = " button " value = " 缓存 " onclick = " c() " >
< input type = " checkbox " name = " test[] " value = " 0 " > 测试测试 < br >
< script language = " JavaScript " >
<!--
var ca = new Array();
function a()
{
var m = document.getElementsByName( ' test[] ' );
for ( var i = 0 ; i < m.length ; i ++ )
{
m[i].checked
== true
? m[i].checked = false
: m[i].checked
= true ;
}
}

function b()
{
var m = document.getElementsByName( ' test[] ' );
var l = m.length;
for ( var i = 0 ; i < l; i ++ )
{
m[i].checked
== true
? m[i].checked = false
: m[i].checked
= true ;
}
}

function c()
{
if (ca.length == 0 )
{
ca
= cache();
}
cl
= ca.length;
for ( var i = 0 ; i < cl; i ++ )
{
ca[i].checked
== true
? ca[i].checked = false
: ca[i].checked
= true ;
}
}

function cache()
{
var m = document.getElementsByName( ' test[] ' );
var cache = new Array();
var l = m.length;
for ( var i = 0 ; i < l; i ++ )
{
cache[i]
= m[i];
}
return cache;
}

// -->
< / script>

 

上面的例子还是比较方便的,因为,checkbox的 name值都一样,如果不一样怎么办???
只能这样

代码
   
     
< script language = " JavaScript " >
<!--
function check()
{
var inputs = document.getElementsByTagName( ' input ' );
var inputsLen = inputs.length;
for ( var i = 0 ; i < inputsLen ; i ++ )
{
if (inputs[i].type.toLowerCase() == ' checkbox ' )
{
inputs[i].checked
== true
? inputs[i].checked = false
: inputs[i].checked
= true ;
}
}
}
// -->
< / script>

 

你可能感兴趣的:(checkbox)