jquery implementation checkbox full selection, reverse selection, all do not select the function code (odd) _jquery

Source: Internet
Author: User
Set the page like the next set of check boxes and a few related buttons (select all, reverse, select all, etc.):
Copy Code code as follows:

<input type= "checkbox" name= "fruit" value= "apple"/> Apples
<input type= "checkbox" name= "fruit" value= "orange"/> Orange
<input type= "checkbox" name= "fruit" value= "banana"/> Banana
<input type= "checkbox" name= "fruit" value= "grape"/> Grapes
<input type= "button" id= "BTN1" value= "Select All" >
<input type= "button" id= "BTN2" value= "all do not choose" >
<input type= "button" id= "Btn3" value= "anti-selection" >
<input type= "button" id= "Btn4" value= "Select All Odd" >
<input type= "button" id= "Btn5" value= "Get All values selected" >

The complete code for implementing the related functionality is as follows:
Copy Code code as follows:

$ (function () {
$ (' #btn1 '). Click (function () {//All selected
$ ("[name= ' Fruit ']"). attr (' checked ', ' true ');
});
$ (' #btn2 '). Click (function () {//All not selected
$ ("[name= ' Fruit ']"). Removeattr (' checked ');
});
$ (' #btn3 '). Click (function () {//anti-election
$ ("[name= ' Fruit ']"). each (function () {
if ($ (this). attr (' checked ')) {
$ (this). Removeattr (' checked ');
}else{
$ (this). attr (' checked ', ' true ');
}
})
});
$ ("#btn4"). Click (function () {//Select all odd
$ ("[name= ' Fruit ']:even"). attr (' checked ', ' true ');
})
$ ("#btn5"). Click (function () {//Get the value of all selected options
var checkval= ';
$ ("[name= ' Fruit '][checked]"). each (function () {
checkval+=$ (This). Val () + ', ';
})
alert (checkval);
})
});

Note that jquery packets must be introduced before using jquery!

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.