电脑技术学习

javaScript实现checkbox多项选择

dn001

其实挺简单的,刚好今天有用到,顺手记一下,以后用就copy好了 .;

<script language="javaScript">

//全选或全部不选
function checkall(){
var isChecked=(document.ck.checkAll.checked == true);
var elements=document.ck.elements;
var counter=elements.length;
for(i=0;i<counter;i++){
var element=elements[i];
if(element.type=="checkbox"){
element.checked=isChecked;

}
}
}

//是否有checkbox被选中
function anyChecked(){

var elements=document.ck.elements;
var counter=elements.length;
for(i=0;i<counter;i++){
var element=elements[i];
if(element.checked == true){
return true;
}
}
return false;
}
</script>

<form name="ck" Method="post" action="a.asp" onsubmit="return anyChecked()">

全选:<input type="checkbox" id="checkAll"onclick="checkall()" />
多项选择框列表:
<input type="checkbox" name="cb" value="1"/>
<input type="checkbox" name="cb" value="2"/>
<input type="checkbox" name="cb" value="3"/>
<input name="Submit" type="submit" value="submit">
</form>

选中之后怎么取它们的值呢?别急,在ASP中取值真是太简单了。如下:;

dim array_key

array_key= Split(Request.Form("cb"),",")

for each m in array_key
response.write m
next