Encounter Toggle () conflict with checkbox in jquery, use other methods to achieve the same effect (work note)

Source: Internet
Author: User

As shown in the figure above, (using the table plugin in jquery) The effect I wanted was: click the checkbox (id "all") in the upper-left corner, and all the checkboxes below (id "CB") will all be selected. The jquery code I started with was this:

$ ("#all"). Toggle (function () {
$ (": CheckBox"). attr ("Checked", "true");},
function () {
$ ("Table:checkbox"). Removeattr ("checked");}
);

The effect of these codes: Click the checkbox in the upper-left corner (id "all"), and all the checkboxes below (id "CB") will all be selected, and once again click on the checkbox (id "all") in the upper-left corner, all the checkboxes below ( ID "CB") will all be canceled. However, the checkbox in the upper-left corner (id "all") never appears. Online Search, the reason is:

The default action is blocked in the toggle method
In the jquery source code, toggle called Event.preventdefault () when registering the click, which is to touch the default event that was originally bound. The problem, then, is clearly that the event that was clicked with the checkbox was brutally obliterated by jquery.

After a long time, finally let me use other methods to achieve the same effect:

The code is as follows:

$ ("#all"). Click (Function (event) {
if (($ ("#all"). attr ("checked")) = = True) {
$ ("INPUT[TYPE=CHECKBOX][NAME=CB]"). each (function () {
$ (this). attr ("Checked", "true");
});
Event.stoppropagation ();
}

As for the reason for the bubbling event in the checkbox, I don't know. When the code actually does this: click the checkbox in the upper-left corner (id "all"), and all the checkboxes below (id "CB") will all be selected, and the checkbox in the upper-left corner (id "all") appears Again, click the checkbox (id "all") in the upper-left corner, and all of the checkboxes below (id "CB") will all be canceled, and the checkbox in the upper-left corner (id "all") disappears. As for the reason in the discussion "" "" "" "" "" "" "

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.