jquery Click Tr to implement checkbox

Source: Internet
Author: User

Title description is a bit inappropriate, but I hope you can understand, in order to more image of the expression, I specially recorded a GIF animated picture.

I do not know 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; do not have to point the check button to achieve;

Implementation of the process a bit tangled, tried a few times did not, and finally used a stupid method, is to click on the line, let his child element (TD) Background color 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)

Maybe you will ask me, then how do you judge the status of the checkbox is not checked (tick status) Ah?

In fact, I didn't even go to judge it .... I hope you don't spray me. I'm just judging. The background color of the sub-element (TD) of the selected row and the background color of the document.body are not the same, if the same, let checkbox.checked=true, not the same let checkbox.checked=false.

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

The method used in jquery:

First (): element one;

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

Children (): Find child elements;

Toggleclass (); Toggle Style

attr (): Adds the checked attribute to the checkbox;

The main implementation of the code:

$(function () {            //adds a Click event to all rows except the table header (the first row).$ ("tr"). First (). Nextall (). Click (function () {                //Toggle style For this line of clicks bgred Code: Background-color: #FF0000;$( This). Children (). Toggleclass ("bgred"); //determines whether the background color of the TD tag is the same as the background color of the body;                if($( This). Children (). CSS ("Background-color")! = $ (document.body). CSS ("Background-color")) {                    //if the same, checkbox.checked=true;$( This). Children (). First (). Children (). attr ("Checked",true); } Else {                    //if different, checkbox.checked=false;$( This). Children (). First (). Children (). attr ("Checked",false);       }            }); });

jquery Click Tr to implement 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.