CheckBox Select All/uncheck/reverse (jQuery v1.11.3 test pass)

Source: Internet
Author: User

Main functions:

Mode one: Use three buttons (select all, all uncheck, deselect) to control the selection of a checkbox. (It is possible to select all/None of the shared buttons, not done here)

Mode two: Use a master checkbox to control the selection state of the other checkbox, while the Controlled checkbox is fully selected and fed back to the Master checkbox (two ways are implemented here).

The following code and implementation results in the jquery v1.11.3 test pass! 2015/07/18!

Test results:

Code:

<!---------------------------------------------------------------------->
<script>
Select All/not select/reverse select; In the jquery 1.11.3 test passed
function Checkedall (checkboxname) {$ ("input[name=" + Checkboxname + "]"). Prop ("checked", true);}
function Checkedno (checkboxname) {$ ("input[name=" + Checkboxname + "]"). Prop ("checked", false);}
function Checkedrev (checkboxname) {$ ("input[name=" + Checkboxname + "]"). each (function () {this.checked =!this.checked;});}
</script>
<input type= "button" class= "btn Btn-xs" name= "Btncheckedall" value= "Select All" onclick= "Checkedall (' ckb ');" >
<input type= "button" class= "btn Btn-xs" name= "Btncheckedno" value= "All do not select" onclick= "Checkedno (' ckb ');" >
<input type= "button" class= "btn Btn-xs" name= "Btncheckedrev" value= "reverse select" onclick= "Checkedrev (' ckb ');" >
<br>
<input type= "checkbox" Name= "ckb" value= "Ckb1" >
<input type= "checkbox" Name= "ckb" value= "Ckb2" >
<input type= "checkbox" Name= "ckb" value= "Ckb3" >
<input type= "checkbox" Name= "ckb" value= "Ckb4" >


<!---------------------------------------------------------------------->
<script>
In the jquery 1.11.3 test passed
Use the checkbox with ID Controlerid to control the selection state of all the checkboxes for name Checkboxname.
The onclick event of the main control checkbox uses some methods
function Checkedctrl (controlerid,checkboxname) {
$ ("input[name=" +checkboxname+ "]"). Prop ("Checked", $ ("#" +controlerid). Prop ("checked"));
}
The Controlled checkbox enables this method to give feedback to the checkbox with ID Controlerid to the selection state of all the checkboxes named Checkboxname.
function Feedbackcheck (controlerid,checkboxname) {
if ($ ("input[name=" +checkboxname+ "]:checked"). Length = = $ ("input[name=" +checkboxname+ "]"). Length) {
$ ("#" +controlerid). Prop ("checked", true)//When the element is all selected, control all of the checkbox is also selected
} else {
$ ("#" +controlerid). Prop ("checked", false); As long as there is a checkbox that has no option to control the selection, it won't checked.
}
The above feedback only select all or not all two states, can increase the judgment feedback all select/All not select/select part of three states
}
</script>
<!--the main control checkbox--
<input type= "checkbox" id= "Checkall" value= "onclick=" Checkedctrl (' Checkall ', ' Checkboxitem '); "><br>
<!--control checkbox--
<input type= "checkbox" Name= "Checkboxitem" value= "checkboxItem1" onclick= "Feedbackcheck (' Checkall ', ' Checkboxitem '); " >
<input type= "checkbox" Name= "Checkboxitem" value= "checkboxItem2" onclick= "Feedbackcheck (' Checkall ', ' Checkboxitem '); " >
<input type= "checkbox" Name= "Checkboxitem" value= "CheckboxItem3" onclick= "Feedbackcheck (' Checkall ', ' Checkboxitem '); " >
<input type= "checkbox" Name= "Checkboxitem" value= "checkboxItem4" onclick= "Feedbackcheck (' Checkall ', ' Checkboxitem '); " >
<br>


<!---------------------------------------------------------------------->
<script>
$ (function () {
var controlerid= "leader"; ID attribute value of the Master checkbox
var checkboxitemname= "checkbox";//The Name property value of the Controlled checkbox
$ ("#" +controlerid). Click (function () {
$ ("input[name=" +checkboxitemname+ "]"). Prop ("Checked", $ (this). Prop ("checked"));
});
$ ("input[name=" +checkboxitemname+ "]"). each (function () {
$ (this). Click (function () {
if ($ ("input[name=" +checkboxitemname+ "]:checked"). Length = = $ ("input[name=" +checkboxitemname+ "]"). Length) {
$ ("#" +controlerid). Prop ("checked", true)//The Master checkbox is selected after all selected in the Controlled checkbox
} else {
$ ("#" +controlerid). Prop ("checked", false); The Master checkbox is not selected as long as the Controlled checkbox has one
}
If necessary, can be changed to feedback Select all/select All/Choose part of three states
})
});
});
</script>
<input type= "checkbox" id= "leader" value= "" ><br>
<input type= "checkbox" name= "checkbox" value= "CheckBox1" >
<input type= "checkbox" name= "checkbox" value= "CheckBox2" >
<input type= "checkbox" name= "checkbox" value= "Checkbox3" >
<input type= "checkbox" name= "checkbox" value= "Checkbox4" >

CheckBox Select All/uncheck/reverse (jQuery v1.11.3 test pass)

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.