1. 程式人生 > >jQuery單擊記錄行自動選中checkbox

jQuery單擊記錄行自動選中checkbox

 $(".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);
                }
            });
       });