0

我有以下 HTML 表格。每行包含一个复选框,后跟一些文本。

<table>
    <tr>
        <td><input type="checkbox" /></td>
        <td>Name</td>
    </tr>
    <tr>
        <td><input type="checkbox" /></td>
        <td>Name2</td>
    </tr>
</table>

我需要一些帮助来编写一个脚本,该脚本将突出显示(添加一个类)已选中的行,取消选中将删除该类。

4

6 回答 6

2
$("#yourTableId input:checkbox").change(function() {
        var $this = $(this);
        if ($this.is(":checked")) {
            $this.closest("tr").addClass("highlight");
        } else {
            $this.closest("tr").removeClass("highlight");
        }
    });
于 2012-07-08T01:57:45.470 回答
2

http://codebins.com/codes/home/4ldqpb8

    $(":checkbox").change(function() {
        $(this).closest("tr").toggleClass("highlight", this.checked)
    })
于 2012-07-08T02:04:08.463 回答
0
$('[type="checkbox"]').change(function(){
  $(this).parents('tr').toggleClass('highlight')
})
于 2012-07-08T01:51:28.300 回答
0

演示 http://jsfiddle.net/328QV/

代码

$(document).ready(function() {
    $("#Table input").click(function() {
        if ($(this).is(":checked")) {
            $(this).parent().parent().addClass("highlight");
        } else {
            $(this).parent().parent().removeClass("highlight");
        }
    });
});​
于 2012-07-08T01:51:58.973 回答
0

看看这个小提琴

JS

$(document).ready(function () {
    $("input:checkbox").change(function () {
        alert();
        if ($(this).prop("checked") == true) {
            $(this).closest('tr').addClass("checked");
        } else $(this).closest('tr').removeClass("checked");
    });
});

CSS

.checked {
    background-color:red;
}
于 2015-04-13T09:16:01.050 回答
0

为我工作

      $(document).on('click','tr',function(){
    if ($(this).css('background-color')=="rgb(143, 162, 225)") {

        $(this).find('input[type="checkbox"]').prop('checked',false);

        $(this).css('background','#ffffff'); 
    } else{
        $(this).find('input[type="checkbox"]').prop('checked',true);

        $(this).css('background','#8FA2E1'); `enter code here`
    }
});
于 2020-05-03T09:27:45.937 回答