<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="JavaScript">
function checkboxCount(name)
{
var count = 0;
var objs = document.getElementsByName(name);
for(var i = 0; i < objs.length; i++)
{
if(objs[i].tagName == 'INPUT' && objs[i].type == 'checkbox')
if(objs[i].checked) count ++;
} return count;
}function check()
{
var count = checkboxCount('MyCheckBox');
if(count < 1)
{
alert("要选东西呀!");
}
else if(count > 10)
{
alert("太贪了吧!");
}
else
{
alert("通过啦!");
}
}
</script>
</HEAD><BODY>
<input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><br>
<input type="button" value="检查" onclick="check();">
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="JavaScript">
function checkboxCount(name)
{
var count = 0;
var objs = document.getElementsByName(name);
for(var i = 0; i < objs.length; i++)
{
if(objs[i].tagName == 'INPUT' && objs[i].type == 'checkbox')
if(objs[i].checked) count ++;
} return count;
}function check()
{
var count = checkboxCount('MyCheckBox');
if(count < 1)
{
alert("要选东西呀!");
}
else if(count > 10)
{
alert("太贪了吧!");
}
else
{
alert("通过啦!");
}
}
</script>
</HEAD><BODY>
<input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><input type="checkbox" name="MyCheckBox"><br>
<input type="button" value="检查" onclick="check();">
</BODY>
</HTML>
方法一:
<script>
function check(obj)
{
var j=0;
var el = f1.elements;
var count = el.length;
for (i=0;i<count;i++)
{
if (el[i].type=="checkbox" && el[i].checked==true)
j++;
}
if(j>5)
{
alert("最多只能選取5個");
obj.checked=false;
}
}
</script>
<form name="f1" action="" method="post">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
</form>
<script>
function check(obj)
{
var j=0;
var el = f1.elements;
var count = el.length;
var obj=event.srcElement;
for (i=0;i<count;i++)
{
if (el[i].type=="checkbox" && el[i].checked==true)
j++;
}
if(j>5)
{
alert("最多只能選取5個");
obj.checked=false;
}
}
</script>
<form name="f1" action="" method="post" onclick="check()">
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
</form>
<SCRIPT LANGUAGE="JavaScript">
var c=0,limit=5;
function limitCheck(obj) {
obj.checked?c++:c--;
if(c>limit)obj.checked=false,c--;
}
</SCRIPT>
<form>
<script>
for (var i=0;i<20;i++) {
document.write("<input type='checkbox' name='xx' onclick='limitCheck(this)'>")
}
</script>
</form>
var j=0
function check(obj)
{ obj.checked==true
{
j=j+1;
}
else
{
j=j-1;
}
}
if(j>5)
{
alert("最多只能選取5個");
obj.checked=false;
}
}
</script>
<form name="f1" action="" method="post">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
<input type="checkbox" onclick="check(this)">
</form>
方法四:
<script>
var checked=new Array()
function check()
{var i,j=0;
var el = f1.elements;
var count = el.length;
var obj=event.srcElement;
checked[checked.length]=obj;
//alert(checked[checked.length])
for (i=0;i<count;i++)
if (el[i].type=="checkbox" && el[i].checked==true)
j++;
if(j>5)
{alert("最多只能選取5個");
checked[checked.length-1].checked=false;
}
}
</script>
<form name="f1" action="" method="post" onclick="check()">
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
<input type="checkbox" >
</form>
这几个程序不仅限制数量, 还让你最后选定的也不能选.
//函数功能:判断当前CheckBox被选中的个数
//函数参数:itemname,表单项的名字
//函数返回:返回被选中的个数
function readcheckboxcount(itemname) {
var thecheckboxcount;
thecheckboxcount=0;
var bootobject=document.all.tags("INPUT");
for (i=0;i<bootobject.length;i++) {
if (bootobject[i].name==itemname && bootobject[i].checked) {
thecheckboxcount+=1;
}
}
return(thecheckboxcount);
}