选中CheckBox时突出显示表数据,取消选中时不突出显示

时间:2010-09-17 04:29:07

标签: javascript jquery html jquery-selectors

我在表格中有以下复选框列表。我想在选中复选框时将<td>设为hilight,而在取消选中复选框时显示为unhilight。我知道我需要将课程添加到<td&gt;选中复选框时,取消选中时删除课程。

<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 个答案:

答案 0 :(得分: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/