Jquery click the subcheck box to change the status of the upper-level check box

Source: Internet
Author: User

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> variable in jquery attributes </title>
<Script type = "text/javascript" src = "jquery-1.3.1.js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
Var zc = "zi". length; // zi in the class attribute class identity is the sub-check box. The length of the character string is the same as the fu length of the identifier. Here,
Checkbox. click (function () {// click the check box to trigger the event function
Var checkbox = $ (this );
Var name = $ (this). attr ("class ");
Var b1 = name. substring (zc, name. length); // obtain the number combination with the suffix of the check box you clicked.
Var bb = $ (this). attr ("checked"); // determines whether the selected or unselected value is true or false.
Var B = "fu" + b1; // combination of the parent check box class
Var a = "zi" + b1; // a combination of sub-check box class
Var b22 = $ ('td input [class = "'+ B +'"] '); // obtain the parent check box using the property Selector
If (bb = true ){
$(B22 [0]). attr ("checked", true); // when selected, the parent check box is also selected
} Else if (bb = false ){

Var box = $ ('td input [class = "'+ a +'"]: checked '); // uncheck this option to check whether the number of selected instances is equal to zero. Uncheck this option when the number of selected instances is greater than zero.
Var cd = box. length;

If (cd = 0 ){
$(B22 [0]). attr ("checked", false );
} Else if (cd> 0 ){
$(B22 [0]). attr ("checked", true );
}
}

});
});
</Script>.
</Head>

<Body>
<Table width = "200" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<Td scope = "col"> Parent selection box <input type = "checkbox" class = "fu1"/> & nbsp; </td>
</Tr>
<Tr>
<Td> Apple <input type = "checkbox" class = "zi1"/> </td>
</Tr>
<Tr>
<Td> banana <input type = "checkbox" class = "zi1"/> </td>
</Tr>
<Tr>
<Td> orange <input type = "checkbox" class = "zi1"> </td>
</Tr>
 
<Tr>
<Td scope = "col"> Parent selection box <input type = "checkbox" class = "fu2"/> & nbsp; </td>
</Tr>
<Tr>
<Td> Apple <input type = "checkbox" class = "zi2"/> </td>
</Tr>
<Tr>
<Td> banana <input type = "checkbox" class = "zi2"/> </td>
</Tr>
<Tr>
<Td> orange <input type = "checkbox" class = "zi2"> </td>
</Tr>
</Table>

</Body>
</Html>


This method is more efficient


Another method is as follows: loop traversal is used for comparison. If there are many options, the code execution efficiency will be affected. The custom attribute method is used here.


<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script type = "text/javascript" src = "jquery-1.3.1.js"> </script>
<Script type = "text/javascript">
$ (Function (){
$ ('[Type = checkbox]'). click (function (){
$ ('. Fu'). each (function (){
Var sign = $ (this). attr ('sign ');
Var flag = true;
$ ('. Zi' + sign). each (function (){
// Alert ($ (this). attr ('names '));
If ($ (this). attr ("checked ")){
// Alert ('aaa ');
// Alert ("dddd" + sign );
Flag = false;

}

});
If (flag ){
// Alert ($ (this). attr ('checked '));
$ (This). attr ('checked', false );
// Alert ("dddd" + sign );
Flag = true;
} Else {
$ (This). attr ('checked', true );
Flag = true;
}


});});

});
</Script>.
</Head>

<Body>
<Table width = "200" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
<Td scope = "col"> Parent selection box <input type = "checkbox" class = "fu" sign = "1"/> & nbsp; </td>
</Tr>
<Tr>
<Td> Apple <input type = "checkbox" class = "zi1" names = "apple"/> </td>
</Tr>
<Tr>
<Td> banana <input type = "checkbox" class = "zi1" names = "banana"/> </td>
</Tr>
<Tr>
<Td> orange <input type = "checkbox" class = "zi1" names = "orange"/> </td>
</Tr>
 
<Tr>
<Td scope = "col"> Parent selection box <input type = "checkbox" class = "fu" sign = "2"/> & nbsp; </td>
</Tr>
<Tr>
<Td> Apple <input type = "checkbox" class = "zi2" names = "apple"/> </td>
</Tr>
<Tr>
<Td> banana <input type = "checkbox" class = "zi2" names = "banana"/> </td>
</Tr>
<Tr>
<Td> orange <input type = "checkbox" class = "zi2" names = "orange"> </td>
</Tr>
</Table>

</Body>
</Html>

Author "highfly-s"
 

Related Article

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.