Discussion on All-choice derivative issues of checkbox

Source: Internet
Author: User

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

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.