Today, the project encountered a very simple requirement: select and cancel all functions, Pa. The Code is as follows:
<SCRIPT type = "text/JavaScript" src = "http://static.joekoe.cn/images/script/jquery.min.js"> </SCRIPT> </code>
<Label> <input id = "checkall" type = "checkbox"/> select all </label>
<Input type = "checkbox" name = "checkbox"/> Item 1
<Input type = "checkbox" name = "checkbox"/> Item 2
<Input type = "checkbox" name = "checkbox"/> Item 3
<Input type = "checkbox" name = "checkbox"/> Item 4
<SCRIPT type = "text/JavaScript"> // <! [CDATA [
$ (Function (){
VaR $ checkbox = $ ('input [name = "checkbox"] ');
$ ("# Checkall"). Click (function (){
$ Checkbox. ATTR ("checked", this. Checked );
});
$ Checkbox. Click (function (){
$ ("# Checkall"). ATTR ("checked", $ checkbox. Length ==$ ("input [name = 'checkbox']: checked"). length? True: false );
});
})
Jquery is the latest version. When you click "select all checkbox" for the third time, the "select all" option disappears. After the page is refreshed, it works normally. When you reference the old version of jquery (1.4), everything works normally, it can be determined that it is a compatibility issue. I learned the following knowledge points after I asked du Niang and learned from the majority of users:
For Boolean attributes, jquery1.6 and later use the. Prop () method.
This Boolean attribute indicates that the attribute value is only true | false.
You can also use the. Prop () method to add only the attribute name and do not need to write the attribute value. For example, checked and disable are actually Boolean attributes.
It is easy to locate the problem and solve the problem. The code is shown as follows:
<SCRIPT type = "text/JavaScript">
$ (Function (){
VaR $ checkbox = $ ('input [name = "checkbox"] ');
$ ("# Checkall"). Click (function (){
$ Checkbox. Prop ("checked", this. Checked );
});
$ Checkbox. Click (function (){
$ ("# Checkall"). Prop ("checked", $ checkbox. Length ==$ ("input [name = 'checkbox']: checked"). length? True: false );
});
})
</SCRIPT>
Discussion on All-choice derivative issues of checkbox