JS get the number of checkbox simple example _javascript skill

Source: Internet
Author: User

JS gets the number of marquee checkboxes selected.

var checkbox = Document.getelementsbyname ("likes[]"); 
This is the way to get multiple-selection boxes to be arrays.
                                            
//like is name = "like[]" and must be added []
var checked_counts = 0;

for (Var i=0;i<checkbox.length;i++) {
if (checkbox[i].checked) {     //Selected checkbox
checked_counts++
}

alert (checked_counts);

What I do is that every click on a multiple selection box to determine whether the current number of checked more than a certain number

function Checkdate () { 
  var n = $ ("#cart_q_num"). Val (); 
   var checkedcount=0; 
   var checkbox = Document.getelementsbyname ("tie_in[]");
   alert (checkbox.length);
   for (Var i=0;i<checkbox.length i + +) { 
   if (checkbox[i].checked) { 
     checkedcount++ 

     }} 
   } 
   alert (checkedcount);
    if (checkedcount>n) { 
       alert ("The quantity of the gifts should equal to the quantity of the sunglasses set."); 
      return false; 
   } else{
     $ ("#free_pro_selected_num"). HTML (checkedcount);
    }

To make the function checkdata () Work every click, you need to add the onclick event in the checkbox box:

 
 

The above JS to get the number of the checkbox is a simple example is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.