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>