當需要選中或取消選中該行中的複選框時,需要更改整行的樣式。基於複選框更改樣式w/jQuery
function myFunc() {
if ($(".chkbx:checked").length) {
$(this).parent().parent().css("background-color","red");
} else {
$(this).parent().parent().css("background-color","green");
}
}
myFunc();
$(".chkbx").change(myFunc);
<table>
<tr>
<td><input type="checkbox" class="chkbx" checked></td>
<td>label 1</td>
</tr>
<tr>
<td><input type="checkbox" class="chkbx" checked></td>
<td>label 1</td>
</tr>
<tr>
<td><input type="checkbox" class="chkbx" checked></td>
<td>label 1</td>
</tr>
</table>
我覺得我失去了一些東西......
沒有什麼不對,但通常您會在使用jQuery框架時看到以$爲起點的jQuery對象開始的變量。 '$ color'使得它看起來好像該變量正在尊重jQuery,在我看來,'color'將是一個更好的選擇,因爲它只是一個字符串。 – Justin808