Js limits the number of checkbox selections and the number of jscheckboxes
Today, when I was working on a project, I encountered a problem. I need to display multiple checkbox check boxes, but I can only select up to six. After debugging for a long time, I finally came out. The Code is as follows:
<SCRIPT LANGUAGE="JavaScript"> var c=0,limit=6; function doCheck(obj) { obj.checked?c++:c--; if(c>limit){ obj.checked=false; alert("over"); c--; }} </SCRIPT>
The result is as follows:
I would like to share with you the hope to help you
How can I use JS to determine the number of checkbox selections for a single table?
<Input type = "checkbox" name = "apple" value = "1">
<Input type = "checkbox" name = "apple" value = "1">
<Input type = "checkbox" name = "apple" value = "1">
<Input type = "checkbox" name = "apple" value = "1">
Child ~~ The name of the Multi-choice box is generally the same. Where did you learn ..
If the name is different, it is not the same group of options, and it is not necessary to determine how many
Function checkboxNum (){
Var checkbox = document. getElementByName ("apple ");
Var j = 0; // number of options selected by the user
For (var I = 0; I <checkbox. length; I ++ ){
If (checkbox [I]. checked ){
J ++;
}
If (I> 6 ){
Break;
}
}
Alert ("the number of options cannot exceed 6 ");
}
How does js obtain the number of check boxes selected by name = checkbox []?
Var inputs = document. getElementsByName ("cb []");
Var checked_counts = 0;
For (var I = 0; I <inputs. length; I ++ ){
If (inputs [I]. checked ){
Checked_counts ++;
}
}
Alert (checked_counts );