var i=0;
for(var j=0;j<document.all.length;j++)
{
if(document.all(i).type == 'checkbox' && document.all(i).checked == true)
i++;
if(i == 3) alert('选太多了');
break;
}...
for(var j=0;j<document.all.length;j++)
{
if(document.all(i).type == 'checkbox' && document.all(i).checked == true)
i++;
if(i == 3) alert('选太多了');
break;
}...
{
alert('选太多了');
break;
}
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<input type="checkbox"/>
<script>
var checkedCount = 0;
function a(){
if (event.srcElement.checked){
checkedCount++;
}else{
checkedCount--;
}
if (checkedCount > 2){
checkedCount--;
alert('最多只能选择两项!');
return false;
}
}
var chks = document.getElementsByTagName("input");
for (var i=0;i<chks.length;i++){
var chk=chks[i];
if (chk.type=="checkbox"){
chk.onclick = a;
}
}
</script>
<td height="22">
<input name="a" type="checkbox" id="a" value="1">
a</td>
<td height="22">
<input name="b" type="checkbox" id="b" value="1">
b</td>
<td height="22">
<input name="c" type="checkbox" id="c" value="1">
c</td>
<td height="22">
<input name="d" type="checkbox" id="d" value="1">
d</td>
</tr>上面的这样结构应该用哪个控制呢?