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 に答える