<script> var max = 3 function chk(obj1){var obj = document.getElementsByName(obj1.name);var j = 0; if (obj.length){ for (i = 0; i < obj.length; i++){ if (obj[i].checked) j++; if (j > max){ alert("对不起,只能选取"+ max +"。"); obj1.checked = false ; break; } } } }</script>这个代码又该如何改呢?
var objs=document.getElementsByName("aaa") var j=0; for(var i=0;i<objs.length;i++) { if(objs[i].checked) j++; } if(parseInt(j)!=3) { alert("错误") }
var objs=document.getElementsByName("aaa") var j=0; for(var i=0;i<objs.length;i++) { if(objs[i].checked) j++; } if(j!=3) { alert("错误") }
在提交中加 var obj = document.getElementById("aaaa"); if(obj.length<3){ alert("所选择的数量小于3"); }
var j=0;
for(var i=0;i<objs.length;i++)
{
if(objs[i].checked)
j++;
}
if(parseInt(j)>3)
{
alert("错误")
}
<script>
var max =3
var j = 0;
function chk(obj1){
var obj = document.getElementsByName(obj1.name);
if(obj1.checked){
j++;
if(j==max){
for (i = 0; i < obj.length; i++){
if(obj[i].checked==false){
obj[i].disabled=true;
}
}
}
}
else{
j--;
for(var i=0;i<obj.length;i++){
if(obj[i].disabled==true){
obj[i].disabled=false;
}
}
}
}
</script>
<INPUT type="checkbox" name="uid1" value="1" onClick="chk(this);">
<INPUT type="checkbox" name="uid1" value="2" onClick="chk(this);">
<INPUT type="checkbox" name="uid1" value="3" onClick="chk(this);">
<INPUT type="checkbox" name="uid1" value="4" onClick="chk(this);">
<INPUT type="checkbox" name="uid1" value="5" onClick="chk(this);">
<!--
function check_form(fN){
for(var i=1;i<=3;i++){
if(i==1) if(!maxSelected(fN,i,2)) return false;//第1题限选2项
if(i==3) if(!maxSelected(fN,i,3)) return false;//第3题限选3项
}
alert('提交!');
}function maxSelected(fN,qN,max){
var objQ = eval('document.'+fN+'.q'+qN),selectedN=0;
if(objQ && objQ.length){
if(objQ.length<=max) return true;
else{
for(var i=0;i<objQ.length;i++) if(objQ[i].checked) selectedN++;
if(selectedN>max) {alert('题目 '+qN+' 限选 '+max+' 项');objQ[0].focus();return false;}
else return true;
}
}
}
</script>
<form name="question" style="font-size:12px;">
1.题目一:(限选两项)<br>
<input type="checkbox" name="q1" value="1" checked>1111
<input type="checkbox" name="q1" value="2">2222
<input type="checkbox" name="q1" value="3" checked>3333
<input type="checkbox" name="q1" value="4">4444
<input type="checkbox" name="q1" value="5" checked>5555
<input type="checkbox" name="q1" value="61">6666<br><br>
2.题目二:<br>
<input type="checkbox" name="q2" value="1">aaa
<input type="checkbox" name="q2" value="2">bbb
<input type="checkbox" name="q2" value="3">ccc
<input type="checkbox" name="q2" value="4">ddd
<input type="checkbox" name="q2" value="5">eee
<input type="checkbox" name="q2" value="61">fff<br><br>
3.题目二:(限选三项)<br>
<input type="checkbox" name="q3" value="1" checked>AA
<input type="checkbox" name="q3" value="2" checked>BB
<input type="checkbox" name="q3" value="3" checked>CC
<input type="checkbox" name="q3" value="4">DD
<input type="checkbox" name="q3" value="5" checked>EE
<input type="checkbox" name="q3" value="6">FF<br><br>
<input type="button" onclick="check_form('question')" value="提交">
</form>
if(objQ.length<=max) return true;改为:if(objQ.length==max) return true;if(selectedN>max) {alert('题目 '+qN+' 限选 '+max+' 项');objQ[0].focus();return false;}改为:
if(selectedN!=max) {alert('题目 '+qN+' 限选 '+max+' 项');objQ[0].focus();return false;}
var max = 3
function chk(obj1){var obj = document.getElementsByName(obj1.name);var j = 0;
if (obj.length){
for (i = 0; i < obj.length; i++){
if (obj[i].checked) j++;
if (j > max){
alert("对不起,只能选取"+ max +"。");
obj1.checked = false ;
break;
}
}
}
}</script>这个代码又该如何改呢?
var j=0;
for(var i=0;i<objs.length;i++)
{
if(objs[i].checked)
j++;
}
if(parseInt(j)!=3)
{
alert("错误")
}
var j=0;
for(var i=0;i<objs.length;i++)
{
if(objs[i].checked)
j++;
}
if(j!=3)
{
alert("错误")
}
var obj = document.getElementById("aaaa");
if(obj.length<3){
alert("所选择的数量小于3");
}