Document directory
There are 10 check boxes. You can select up to three check boxes. Otherwise, all check boxes are dimmed.
(You can select the check box again .)
Set the variable part as the JS parameter for code reuse.
JS Code
The first parameter is the name of the check box, and the second parameter is the maximum allowed value.
function choicetest(name,num){var choicearr = document.getElementsByName(name);var a=0;for(var i=0;i<choicearr.length;i++)if(choicearr[i].checked){a=a+1;}if(a==num){for(var i=0;i<choicearr.length;i++)if(!choicearr[i].checked)choicearr[i].disabled='disabled';}else{for(var i=0;i<choicearr.length;i++)choicearr[i].removeAttribute('disabled');}}
Sample program
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">