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

发布于 2024-09-19 18:39:22 字数 1756 浏览 5 评论 0 原文

我在表中有以下复选框列表。我想在选中复选框时突出显示 ,并在取消选中复选框时取消突出显示。我知道我需要将类添加到 >;当选中复选框时,取消选中时删除类。

<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>

I have the following check box list inside table. I want to hilight the <td> when a checkbox is checked and unhilight when checkbox is unchecked. I know that I need to add class to <td> when checkbox is checked and remove class when unchecked.

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那小子欠揍 2024-09-26 18:39:50

这是一种方法:

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/

Here is one way to do it:

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

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

Live example: http://www.jsfiddle.net/VTSXz/2/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文