jquery Statistics check box select Sample _jquery

Source: Internet
Author: User
Before I used JS can only judge traversal and then get
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>jquery check box select number </title>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<script type= "Text/javascript" >
Window.onload = function () {//page all elements loaded complete
var btn = document.getElementById ("btn"); Get element with ID btn (button)
Btn.onclick = function () {//To add an onclick event to an element
var arrays = new Array (); Create an Array object
var items = document.getelementsbyname ("check"); Gets a set of elements (checkbox) with Name check
for (i=0 i < items.length i++) {//loop this set of data
if (items[i].checked) {//Decide whether to select
Arrays.push (Items[i].value); Adds the qualifying criteria to the array. Push () is a method in a JavaScript array.
}
}
Alert ("The number selected is:" +arrays.length);
}
}
</script>
<body>
<form method= "POST" action= "#" >
<input type= "checkbox" value= "1" name= "Check" checked= "Checked"/>
<input type= "checkbox" value= "2" name= "Check"/>
<input type= "checkbox" value= "3" name= "Check" checked= "Checked"/>
<input type= "button" value= "you selected number" id= "btn"/>
</form>
</body>

By using jquery to get the number of checkbox items, you need to use the size () method of jquery or the length property, and the following example is the number of checkbox selection through the Length property.
Copy Code code as follows:

<script language= "JScript" >
function Check () {
var boxarray = d Ocument.getelementsbyname (' Obox ');
var total = 0;
for (Var i=0;i<boxarray.length;i++) {
if (boxarray[i].checked) {
total++;
}
}
if (total>0) {
  if window.confirm (' select ' +total+ ' song, do you want to continue?) ') {
    window.open (' About:blank ', ' subwin ', ');
    return true;
 }
  else{
    return false;
 }
}
else{
Window.alert (' No choice! ') ;
return false;
}
}
</script>
<form target= "Subwin" onsubmit= "return check ();" >
<input type= "checkbox" Name= "Obox" > Song one
<input type= "checkbox" Name= "Obox" > Song two
<input t ype= "checkbox" Name= "Obox" > Song three
<input type= "button" value= "Look" >
</form>
 

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.