2010-09-17 76 views
0

我在表格裏面有下面的複選框列表。當複選框被選中時,我想要高亮<td>,並且當複選框未被選中時不高亮。我知道當複選框被選中時,我需要將類添加到<td>,並在未選中時刪除類。當CheckBox被選中時Hilight表數據,未被選中時不被取消

<table id="cbDepartment"> 
    <tbody><tr> 
     <td><input type="checkbox" name="cbDepartment0" id="cbDepartment_0"><label for="cbDepartment_0">Check Box 1</label></td> 
     <td><input type="checkbox" name="cbDepartment1" id="cbDepartment_1"><label for="cbDepartment_1">Check Box 2</label></td> 
     <td><input type="checkbox" name="cbDepartment2" id="cbDepartment_2"><label for="cbDepartment_2">Check Box 3</label></td> 
    </tr><tr> 
     <td><input type="checkbox" name="cbDepartment3" id="cbDepartment_3"><label for="cbDepartment_3">Check Box 4</label></td> 
     <td><input type="checkbox" name="cbDepartment4" id="cbDepartment_4"><label for="cbDepartment_4">Check Box 5</label></td> 
     <td><input type="checkbox" name="cbDepartment5" id="cbDepartment_5"><label for="cbDepartment_5">Check Box 6</label></td> 
    </tr><tr> 
     <td><input type="checkbox" name="cbDepartment6" id="cbDepartment_6"><label for="cbDepartment_6">Check Box 7</label></td> 
     <td><input type="checkbox" name="cbDepartment7" id="cbDepartment_7"><label for="cbDepartment_7">Check Box 8</label></td> 
     <td><input type="checkbox" name="cbDepartment8" id="cbDepartment_8"><label for="cbDepartment_8">Check Box 9</label></td> 
    </tr> 
</tbody> 
</table> 

回答

1

下面是做到這一點的一種方法:

jQuery(function() { 
    $('#cbDepartment :checkbox').click(function() { 
    var cell = $(this).closest('td'); 

    if ($(this).is(':checked')) { 
     cell.addClass('check'); 
    } 
    else { 
     cell.removeClass('check'); 
    } 
    }); 
}); 

活生生的例子:http://www.jsfiddle.net/VTSXz/2/

+0

感謝的這一個。 – Narazana 2010-09-17 04:54:51

相關問題