Js limits the number of checkbox selections and the number of jscheckboxes

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.