jquery Click TR implements the checkbox selection method _jquery

Source: Internet
Author: User

The title describes a bit inappropriate, but I hope you can understand that in order to more image expression, I deliberately recorded a GIF animated picture.

I do not know whether the actual development of the use of this effect, but I personally think that this way more humane, because as long as the point to a line, you can make checkbox.checked=true;

The process of implementation is a bit tangled, tried a few times did not become, and finally used a stupid method, is to click on the line, so that his child elements (TD) Background color is red. (because I used the light bar effect, if I click on the line (TD), the color is changed, but the mouse left the time to change back to the original color again)

Maybe you will ask me, then how do you Judge checkbox state is not checked (tick state) Ah?

In fact, I did not go to judge it .... I hope you don't spray me. I just judged. The background color of the selected row's child elements (TD) is not the same as the background color of the document.body, if the same, let the checkbox.checked=true, not the same let checkbox.checked=false.

Thought is such a train of thought, if who has a better way to paste up, we learn to study together.

The methods used in jquery:

First (): element one;

Nextall (): All elements after XX: mainly to remove the header of the first row

Children (): Find child elements;

Toggleclass (); Toggle Style

attr (): Add checked attribute to checkbox;

The main implementation code:

Copy Code code as follows:

$ (function () {
Add the Click event to all rows except the header (the first line).
$ ("tr"). Nextall (). Click (function () {
Toggles the code in the style bgred for the clicked Line: Background-color: #FF0000;
$ (this). Children (). Toggleclass ("bgred");
To determine whether the background color of TD mark and Body background color is the same;
if ($ (this). Children (). CSS ("Background-color")!= $ (document.body). CSS ("Background-color")) {
If the same, checkbox.checked=true;
$ (this). Children (). (). A (). Children (). attr ("Checked", true);

} else {
if different, checkbox.checked=false;
$ (this). Children (). (). A (). Children (). attr ("checked", false);
}
});
});

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.