我有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