jQueryを使って絞込み検索を作りたいです。
下記コードでor検索はできるようになりました。
全てをor検索ではなく、各hoge-1、hoge-2はor検索、hoge-1とhoge-2間はand検索にしたいです。
例えば、a-2、b-1、b-2にチェックを入れた場合、hogehogeは表示せずにhogeだけ表示したいです。わかりにくくて申し訳ないですがよろしくお願いします。
https://jsfiddle.net/yx92x5bv/1/
###HTML
HTML
1<form> 2 <div class="test"> 3 <div class="hoge-1"> 4 <input type="checkbox" id="a-1"><label class="label" for="a-1">a-1</label> 5 <input type="checkbox" id="a-2"><label class="label" for="a-2">a-2</label> 6 <input type="checkbox" id="a-3"><label class="label" for="a-3">a-3</label> 7 </div> 8 <div class="hoge-2"> 9 <input type="checkbox" id="b-1"><label class="label" for="b-1">b-1</label> 10 <input type="checkbox" id="b-2"><label class="label" for="b-2">b-2</label> 11 <input type="checkbox" id="b-3"><label class="label" for="b-3">b-3</label> 12 </div> 13 </div> 14</form> 15<div class="result"> 16 <section class="a-2 b-1"> 17 <h2>hoge</h2> 18 </section> 19 <section class="a-1 b-2"> 20 <h2>hogehoge</h2> 21 </section> 22 <section class="a-3 b-3"> 23 <h2>hogehogehoge</h2> 24 </section> 25</div>
###jQuery
javascript
1 $(".test input[type=checkbox]").click(function () { 2 showResult(); 3 }); 4 function showResult(){ 5 var showClass = []; 6 $(".test input[type=checkbox]").each(function(){ 7 if($(this).prop('checked')) { 8 showClass.push($(this).prop('id')); 9 } 10 }); 11 $('.result section').hide(); 12 $.each(showClass, function(i, text){ 13 $('.result .'+text).show(); 14 }); 15 16 }
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/25 02:56
2017/07/25 03:18 編集
2017/07/25 05:52