There are 10 checkboxes, and the user can only check at most 3, otherwise all the checkboxes will be grayed out.
(When the user checks off the check box again, he can still select it again.)
Set the variable part as a JS parameter to achieve code reuse.
JS code
The first parameter is the name of the check box, and the second parameter is the maximum allowed check value.
function choicetest(name,num){
var choicearr = document.getElementsByName(name);
var a=0;
for(var i=0;iif(choicearr[i].checked){
a=a 1;
}
if(a==num){
for(var i=0;iif(!choicearr[i]. checked)
choicearr[i].disabled='disabled';
}else{
for(var i=0;ichoicearr[i].removeAttribute( 'disabled');
}
}
Example program < ;meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Untitled Document