gpt4 book ai didi

javascript - 使用/jQuery 根据复选框更改样式

转载 作者:行者123 更新时间:2023-11-28 16:24:56 25 4
gpt4 key购买 nike

当选中或取消选中该行中的复选框时,我需要更改整行的样式。

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>

我想我在这里遗漏了一些东西......

最佳答案

$('.chkbx').change(function(){
var $this = $(this),
color = ($this.is(':checked')) ? 'red' : 'green';
$this.closest('tr').css('background-color', color);
});

演示:http://jsfiddle.net/6dR8C/1/

关于javascript - 使用/jQuery 根据复选框更改样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8319948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com