我有4个复选框,每个都在单独的<div>
。每个div都有相应的类(widthCol1 - > widthCol4)。 每个div在<table>
的单个<tr>
中单独在<td>
内。 根据所选复选框的组合,我需要以特定颜色对表格的行进行着色。jQuery多个条件
考虑到只有前2个复选框,它应该在选中.widthCol2 input
时为#ffaaaa打上颜色,否则为#ccff99。如果没有选中复选框(未实现),它也应该为#fff行着色。
如果在其他行中选择了第二个复选框,则该脚本不起作用。为什么?
$(".widthCol1 input").click(function() {
if ($(this).is(":checked")) {
if ($(".widthCol2 input").is(":checked")) {
$(this).closest("tr").css("background-color", "#ffaaaa");
} else {
$(this).closest("tr").css("background-color", "#ccff99");
}
} else {
// test other checkboxes before coloring #fff
}
});
这个脚本将被用于所有4.是否还有一个更好,更清洁的方式来测试它们?
你能为你的问题创建一个jsFiddle并给出一个链接。 – Vins