Use jquery to get the value of the selected checkbox

Source: Internet
Author: User

Check the Internet, feel some people answer really is unintelligible, or code is not easy. Or some want to install force ingredients and so on.

The following is the value code that gets the input checkbox selected by using jquery:

<meta charset= "GBK" >
<!--introducing jquery--
<script src= "Jquery-1.3.1.js" type= "Text/javascript" ></script>

<body>
<input type= "checkbox" value= "Orange" name= "check" > Orange 1</input>
<input type= "checkbox" value= "Banana" name= "check" > Banana 1</input>
<input type= "checkbox" value= "Watermelon" name= "check" > Watermelon 1</input>
<input type= "checkbox" value= "Mango" name= "check" > Mango 1</input>
<input type= "checkbox" value= "Grape" name= "check" > Grape 1</input>

<input type= "button" value= "Method 1" id= "B1" >
<input type= "button" Value= "Method 2" id= "B2" >


</body>

<script>
Method 1
$ ("#b1"). Click (function () {
$ (' input:checkbox:checked ') equivalent to $ (' input[type=checkbox]:checked ')
Select the checkbox that is selected
$.each ($ (' input:checkbox:checked '), function () {
Window.alert ("You have chosen:" +
$ (' input[type=checkbox]:checked '). length+ ", of which there is:" +$ (This). Val ());
});
});

Method 2
$ ("#b2"). Click (function () {
$.each ($ (' input:checkbox '), function () {
if (this.checked) {
Window.alert ("You have chosen:" +
$ (' input[type=checkbox]:checked '). length+ ", of which there is:" +$ (This). Val ());
}
});
});
</script>

Use jquery to get the value of the selected checkbox

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.