Home > Web Front-end > JS Tutorial > JavaScript sample code to limit the number of checkbox selections_javascript skills

JavaScript sample code to limit the number of checkbox selections_javascript skills

WBOY
Release: 2016-05-16 17:24:27
Original
1077 people have browsed it

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.

Copy code The code is as follows:

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
Copy code The code is as follows:




< ;meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Untitled Document




Choice 1
Choice 2
Choose 5


Choice 6
Choice 7
Choice 8
Choice 9



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template