<! 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"