JS jquery Gets the selected checkbox value separately

Source: Internet
Author: User


Reprint Address http://hi.baidu.com/lipan4/blog/item/8531c62330ee73b04623e886.html

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > <style>

</style>
<title>js gets the value of the check box selected </title>
<body>
<input type= "checkbox" Name= "Aihao" value= "0"/>0&nbsp;&nbsp;
<input type= "checkbox" Name= "Aihao" value= "1"/>1&nbsp;&nbsp;
<input type= "checkbox" Name= "Aihao" value= "2"/>2&nbsp;&nbsp;
<input type= "checkbox" Name= "Aihao" value= "3"/>3&nbsp;&nbsp;
<input type= "checkbox" Name= "Aihao" value= "4"/>4&nbsp;&nbsp;
<input type= "checkbox" Name= "Aihao" value= "5"/>5&nbsp;&nbsp;
<input type= "checkbox" Name= "Aihao" value= "6"/>6&nbsp;&nbsp;
<input type= "checkbox" Name= "Aihao" value= "7"/>7&nbsp;&nbsp;
<input type= "button" onclick= "Chk ()" value= "Submit"/>
</body>
<script src= "Jquery.js" ></script><!--This is a loading jquery.js file that can be removed if you do not use jquery-->
<script>
function Chk () {
var obj=document.getelementsbyname (' Aihao '); Select All Name= "Aihao" objects and return the array
After we take the array of objects, we loop through it to see if it's being checked.
var s= ';
for (var i=0; i<obj.length; i++) {
if (obj[i].checked) s+=obj[i].value+ ', '; If selected, adds value to the variable s
}
So now to detect the value of S, you know the value of the selected checkbox.
Alert (s== '? ') You have not chosen any content yet. ': s);
}

function Jqchk () {//jquery Get check box value
var s= ';
$ (' input[name= ' Aihao ']:checked '). each (function () {
s+=$ (This). Val () + ', ';
});
Alert (s== '? ') You have not chosen any content yet. ': s);
}
</script> Click "Submit", can get the correct choice of the value, but the next one, (English comma), this can be detected and then removed with substring, or get to check box to select the value of the general to be transferred to the number of groups to use, so you can also in the number of groups, Removes the last array element.

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.