<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<button οnclick="getFormsSelectedCheckboxValue('item')">test</button>
<form id="form1" name="form1">
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="451" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="551" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="45451" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="554451" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="551" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="5551" />
</form>
<form id="form2" name="form2">
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="1" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="2" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="3" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="4" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="5" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="6" />
</form>
<form id="form3" name="form3">
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="7" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="8" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="9" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="77" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="55" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="44" />
</form>
<form id="form4" name="form4">
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="33" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="45" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="24" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="254" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="76" />
<input type="checkbox" name="item" οnclick="checkSelectedItemNo('form1','item','2')" value="34" />
</form>
<script type="text/javascript">
/*
*验证form表单复选框是否按指定个数选择
*formName form表单id
*checkboxName 复选框名称
*maxSelected 复选框要求可选的个数
*/
function checkSelectedItemNo(formName,checkboxName,maxSelected) {
var checkedItemNo=0;
var form=document.getElementById(formName);
//alert(form.name);
var elements = form.elements[checkboxName];
if (elements!=null) {
for (var i = 0; i <elements.length; i++) {
var e = elements[i];
if (e.checked) {
checkedItemNo++;
}
}
if(checkedItemNo>maxSelected){
alert("该题可选项数目为:"+maxSelected+",请重新选择!");
for (var i = 0; i <elements.length; i++) {
var e = elements[i];
checkedItemNo=1
e.checked=false;
}
}
}
}
//获得所有form表单指定选中复选框的值
function getFormsSelectedCheckboxValue(checkboxName){
//alert(checkboxName);
var values=new Array();
var index1=0;//为数组定义索引
for(var i=0;i<document.forms.length;i++){
var form=document.forms[i];
var value=new Array();
var index2=0;//为数组定义索引
//遍历指定form表单所有元素
for(var j=0;j<form.length;j++){
var element=form[j];
if(element.type=="checkbox"&&element.name==checkboxName){
if (element.checked) {
if(element.value.length!=0)
value[index2]=element.value;
index2++;//数组索引++
}
}
}
if(value.length!=0){
values[index1]=value;
index1++;//数组索引++
}
}
alert(values);
}
//返回选中复选框的值
function do_action(formName, checkboxName){
var elements =document.getElementsByName(checkboxName);
var values = new Array();
for (var i = 0; i <elements.length; i++) {
var e = elements[i];
if (e.checked) {
values[i] = elements[i].value;
}
}
return values;
}
</script>
</body>
</html>