---
title: "jQuery单击记录行自动选中checkbox"
author: "yyjava"
date: "2013-05-31T11:09:18Z"
tags: []
categories: ["Web前端"]
slug: "jquery-checkbox"
draft: false
lastmod: "2013-05-31T11:09:18Z"
id: 1880496
url: "https://yyjava.iteye.com/blog/1880496"
---

$(".list tr").slice(1).each(function(){   
    var p = this;   
    $(this).children().slice(1).click(function(){   
        $($(p).children()\[0]).children().each(function(){   
            if(this.type=="checkbox"){   
                if(!this.checked){   
                    this.checked = true;   
                }else{   
                    this.checked = false;   
                }   
            }   
        });   
    });   
});  
   
 $(function () {  
            //除了表头（第一行）以外所有的行添加click事件.  
            $("tr").first().nextAll().click(function () {  
                //为点击的这一行切换样式bgRed里的代码：background-color:#FF0000;  
                $(this).children().toggleClass("bgRed");  
                //判断td标记的背景颜色和body的背景颜色是否相同;  
                if ($(this).children().css("background-color") != $(document.body).css("background-color")) {  
                    //如果相同，CheckBox.checked=true;  
                    $(this).children().first().children().attr("checked", true);

                } else {  
                    //如果不同,CheckBox.checked=false;  
                    $(this).children().first().children().attr("checked", false);  
                }  
            });  
       });