それぞれテーブルの行には3つのチェックボックスがあり、それぞれが<td>
の中にあります。異なるテーブル行の複数のチェックボックスから値を取得
チェックボックスがオンになっている特定のルールに応じて、その行に4番目の<td>
を表示したいとします。各行は、このようなものです:
HTML
<tr class="tableRow">
<td>
<input class="select" class="select" type="checkbox" value="select"/>
</td>
<td>
<input class="voice" class="voice" type="checkbox" value="voice">
</td>
<td>
<input class="mail" class="mail" type="checkbox" value="mail"/>
</td>
<!--The <td>'s i want the results of the checkboxes to show -->
<td class="initial-cost">Initial cost</td>
<td class="voicemail-and-initial-cost">VoiceMail and Initial Cost</td>
<td class="email-and-initial-cost">Email and Initial Cost</td>
<td class="all-services-cost">All Services Cost</td>
</tr>
そして、私はちょっと働き、このjQueryの持っているが、それはすべてのテーブルの行を示す "...コスト「のではなく、各行に対してより個別に
jQueryの
$(document).ready(function() { //on page load....
$(".voicemail-and-initial-cost").hide(); //hide all cost elements
$(".email-and-initial-cost").hide(); //hide all cost elements
$(".all-services-cost").hide(); //hide all cost elements
$(".select").change(function(event) { //when #select is clicked...
if ($(this).is(":checked")) { //if this checkbox has been checked...
$(".initial-cost").show(); //show .initial-cost element
} else { //otherwise...
$(".voicemail-and-initial-cost").hide(); //hide all cost elements
$(".email-and-initial-cost").hide(); //hide all cost elements
$(".all-services-cost").hide(); //hide all cost elements
}
});
$(".voice").change(function(event) { //when #voice is clicked...
if ($(this).is(":checked")) { //if this checkbox has been checked...
if ($('.mail').is(":checked")) { //check to see if #mail is checked too and if it is...
$(".initial-cost").hide(); //hide .initial-cost
$(".voicemail-and-initial-cost").hide(); //hide .voicemail-and-initial-cost
$(".email-and-initial-cost").hide();
$(".all-services-cost").show(); //show .all-services-cost
} else { //but if #mail is not checked....
$(".initial-cost").hide(); //hide .initial-cost
$(".voicemail-and-initial-cost").show(); //show .voicemail-and-initial-cost instead
}
} else { //otherwise if this checkbox is not checked...
if(($('.select').is(":checked")) && ($('#mail').is(":checked"))){ //and .select AND .mail is checked however...
$(".initial-cost").hide(); //hide .initial-cost
$(".all-services-cost").hide(); // hide .all-services-cost
$(".email-and-initial-cost").show(); //show .email-and-initial-cost instead
} else if (($('.select').is(":checked")) && (!$('.mail').is(":checked"))){ //otherwise if #select is checked AND #mail is NOT checked....
$(".voicemail-and-initial-cost").hide();
$(".initial-cost").show();
}
}
});
$(".mail").change(function(event) { //when #mail is clicked...
if ($(this).is(":checked")) { //if this checkbox has been checked...
if ($('.voice').is(":checked")) { //check to see if #voice is checked too and if it is...
$(".initial-cost").hide(); //hide .initial-cost
$(".voicemail-and-initial-cost").hide(); //hide .voicemail-and-initial-cost
$(".email-and-initial-cost").hide(); //hide .email-and-initial-cost
$(".all-services-cost").show(); //show .all-services-cost
} else { //but if #voice is not checked....
$(".initial-cost").hide(); //hide .initial-cost
$(".email-and-initial-cost").show(); //show .email-and-initial-cost instead
}
} else { //otherwise if this checkbox is not checked...
if(($('.select').is(":checked")) && ($('.voice').is(":checked"))){ //and #select and #voice is checked however...
$(".all-services-cost").hide(); // hide .all-services-cost
$(".voicemail-and-initial-cost").show(); //show .voicemail-and-initial-cost
} else if (($('.select').is(":checked")) && (!$('.voice').is(":checked"))){ //if this checkbox is not checked AND #voice is NOT checked...
$(".all-services-cost").hide(); // hide .all-services-cost
$(".voicemail-and-initial-cost").hide();
$(".email-and-initial-cost").hide();
$(".initial-cost").show(); //show .initial-cost instead
}
}
});
}); //end of ready
今私はちょうどの要素をターゲットに考え出し問題の行、私がここにあるものから、jQueryのセレクタを変更する必要があります:
$(".initial-cost").hide();
のようなものに:
$('td').next(':has(.initial-cost):first').hide();
しかし、今、私は全く起こって何を取得 - でもないすべてのエラーメッセージコンソール。
ルールそのものが問題ではないことに注意してください。実際には、それらのルールをテーブル行ごとに適用するだけで、すべてを一度に適用する必要はありません。私はこれが理にかなっていて、正しい方向に向いている人がいることを願っています。ここ は、それが役立つはず私が持っているもののJSfiddleです:
正しくと同じ行の別の要素をターゲット$(this).parents('.tableRow').find(".initial-cost").show();
へ
$(".initial-cost").show();
: https://jsfiddle.net/monkeyroboninja/5n0v0eL5/
IDが文書に固有でなければなりません。そのテーブルの行が真に繰り返されると、無効なHTMLがあり、それに対して実行されているコードは機能しません。代わりに、クラスとイベントの委任を使用してください。 –
ああ、あなたはIDについて正しいのですが、今修正しています.... – Liam