<form id=frm>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<script>
function checknum()
{
var num;
num=0;
for (i=0;i<=document.all.frm.elements.length-1;i++)
{
if (document.all.frm.elements[i].checked)
{
num+=1;
if (num>=5)
{
event.returnValue=false;
}
}
}
}
</script>
</form>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<input type=checkbox onclick=checknum()>
<script>
function checknum()
{
var num;
num=0;
for (i=0;i<=document.all.frm.elements.length-1;i++)
{
if (document.all.frm.elements[i].checked)
{
num+=1;
if (num>=5)
{
event.returnValue=false;
}
}
}
}
</script>
</form>
<input onclick="aa()" type=button value=check>
<script>
n=0
function aa()
{
l=document.getElementsByName("a")
for(i=0;i<l.length;i++)
{
if(l[i].checked) n++
}
if(n>5) alert("多于5了")
}
</script>
<input onclick="aa()" type=button value=check>
<script>
n=0
function aa()
{
l=document.getElementsByName("a")
for(i=0;i<l.length;i++)
{
if(l[i].checked) n++
}
if(n>5) alert("多5了")
}
</script>
<input onclick="aa()" type=button value=check>
<script>
function aa()
{
n=0
l=document.getElementsByName("a")
for(i=0;i<l.length;i++)
{
if(l[i].checked) n++
}
if(n>5) alert("多于5了")
}
</script>
要知道<input name=a >这个是会改变的呀。不过写也可以用。 wangyime(我有Web恐惧症)
写的相对实用一点。
<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>
function document.onclick()
{
if(event.srcElement.type=="checkbox")
{
var e = document.form1.elements;
var j = 0;
for(var i=0; i<e.length; i++)
{
if(e[i].type=="checkbox" && e[i].checked) j++
if(j > 5)
{
alert("你只能选择五个");
event.srcElement.checked = false;
break;
}
}
}
}
</script>
<input type=checkbox name=c>
<input type=checkbox name=c>
<input type=checkbox name=c>
<input type=checkbox name=c>
<input type=checkbox name=c>
<input type=checkbox name=c>
<input type=checkbox name=c>
<input type=checkbox name=c>
<script>
document.onclick=check;
function check(){
var num=0;
for (i=0;i<document.all.c.length;i++){
if(document.all.c[i].checked){
num++
if (num>5){
event.returnValue=false;
}
}
}
}
</script>
<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>
<button>button</button>
<script>
var i=0;
document.onclick=function(){
if (event.srcElement.type=="checkbox" && event.srcElement.checked){
if ((i++)>5-1) event.returnValue=false
}
}
</script>
发贴的时候其实就发现这个逻辑错误了。可惜
这里不能修改帖子。我的代码再加一个判断,如果
!checked 那么 --i 就可以了