次のjQueryコードは、そのクラスに1つの要素(<tr>
)しかない場合、選択されたオプションのみを表示するように機能しますが、クラスと一緒に。変更する必要があるものは何ですか?jQuery、複数の選択されたオプションを同じクラスで表示するスイッチケース
$(document).ready(function() {
$("#genre").change(function() {
var id = $(this).find("option:selected").attr("id");
switch (id) {
case "opt_all":
$("tr").show();
break;
case "opt_family":
$(".family").show().siblings().hide();
break;
case "opt_life":
$(".lifestyle").show().siblings().hide();
break;
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name='genre' id='genre'>
<option id='opt_all' selected='selected'>All Genre</option>
<option id='opt_family'>Family & Friends</option>
<option id='opt_life'>Lifestyle</option>
</select>
<table>
<tr class="family"></tr>
<tr class="life"></tr>
<tr class="family"></tr>
</table>
元のセレクタ( 'siblings()'は認識していません)にあります。たとえば、 '.siblings( ':not(.lifestyle)')のようなものを削除したいでしょう。逆にそれを逆にして、最初に隠してから行ってください。それはおそらくもっと簡単です。 –
このように:https://jsfiddle.net/kd07q3bb/対この:https://jsfiddle.net/kd07q3bb/1/ –
あなたの提案をありがとう! ':not(.__)'を追加する!あなたは私の一日を作った。 @Jared Farrish – isosmall