Simulate multi-level check box effects -- jquery, check box -- jquery

Source: Internet
Author: User

Simulate multi-level check box effects -- jquery, check box -- jquery

Today, I realized jquery's strength again. I made a multi-level check box, and the code would be over 20 + lines in total.

I want to use js for a look. I can't write it after writing a few methods, so I have to consider a lot of compatibility, and the amount of code is soaring.

This article mainly describes how jquery achieves this effect. The code block is divided into two parts:

One is the effect of full selection, that is, when you click the full check box, its descendants are selected or not selected. The Code is as follows:

EvtEle. parent (). next (". checks "). find ("input: checkbox "). attr ("checked", evtEle [0]. checked); // evtEle is the clicked check box

Second, the parent box of the current check box determines whether or not the parent box is selected based on whether all the brothers in the current box are selected, and then continues to look up the parent box of the parent box.
When you select all, The implementation here uses parents to obtain all the parent boxes, and completes each operation with each.

If not all are selected, the parent box is deselected in turn. The Code is as follows:

                if (evtEle.is("input:checked")) {                    evtEle.parents(".checks").each(function () {                        !$(this).children("p").children("input:checkbox").filter(function () {                            return !this.checked;                        })[0] && $(this).prev().children("input:checkbox").attr("checked", "checked");                    });                } else {                    evtEle.parents(".checks").prev().children("input:checkbox").attr("checked", false);                }

 

Download demo

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.