前提・実現したいこと
select optionを使った絞り込み検索をinput radioで検索ができるようにしたいです。
select optionのプルダウンを、ラジオボタンに変更したいためです。
しかし、select optionでは問題なく動くのですが、input radioに変更すると動かなくなります。
ご教授お願いします。
HTMLの記述内容
HTML
1<div class="filterArea"> 2 <div class="filter_life_box"> 3 <div class="filter_life"> 4 <select name="life" size="5"> 5 <option value="">ALL</option> 6 <option value="1">TYPE</option> 7 <option value="2">SHOP</option> 8 <option value="3">OFFICE</option> 9 </select> 10 </div> 11 </div> 12 <div class="filter_keyword"> 13 <input class="window" type="text" name="filter_free" size="40" /> <button type='button' name='free_submit'></button> 14 </div> 15 <div class="filter_tag"> 16 <input type="checkbox" name="tag" value="tag1" id="chk01"><label for="chk01">tag1</label> 17 <input type="checkbox" name="tag" value="tag2" id="chk02"><label for="chk02">tag2</label> 18 <input type="checkbox" name="tag" value="tag3" id="chk03"><label for="chk03">tag3</label> 19 <input type="checkbox" name="tag" value="tag4" id="chk04"><label for="chk04">tag4</label> 20 <input type="checkbox" name="tag" value="tag5" id="chk05"><label for="chk05">tag5</label> 21 <input type="checkbox" name="tag" value="tag6" id="chk06"><label for="chk06">tag6</label> 22 <input type="checkbox" name="tag" value="tag7" id="chk07"><label for="chk07">tag7</label> 23 </div> 24<!--/.filterArea--></div> 25<div class="resultArea"> 26 <div class="productCntArea"></div> 27 <div class="productArea"></div> 28<!--/.resultArea--></div>
js
1$(function() { 2 /*================================================================ 3 情報 4 ================================================================*/ 5 var allList = [ 6 { 7 link: "https://www.google.com/", 8 id: "id0001", 9 life: 14, 10 title: "テスト1", 11 tag: ["tag1"], 12 description: "sub text………………………………………………………" 13 }, 14 { 15 link: "https://www.google.com/", 16 id: "id0002", 17 life: 2100, 18 title: "テスト2", 19 tag: ["tag1","tag2"], 20 description: "sub texttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttexttext" 21 }, 22 { 23 link: "https://www.google.com/", 24 id: "id0003", 25 life: 1040, 26 title: "テスト3", 27 tag: ["tag3"], 28 description: "sub text" 29 }, 30 { 31 link: "https://www.google.com/", 32 id: "id0004", 33 life: 2110, 34 title: "テスト4", 35 tag: ["tag1","tag2","tag4"], 36 description: "sub text" 37 }, 38 { 39 link: "https://www.google.com/", 40 id: "id0005", 41 life: 1155, 42 title: "テスト5", 43 tag: ["tag2","tag5"], 44 description: "sub text" 45 }, 46 { 47 link: "https://www.google.com/", 48 id: "id0006", 49 life: 2222, 50 title: "テスト6", 51 tag: ["tag2","tag6"], 52 description: "sub text" 53 }, 54 { 55 link: "https://www.google.com/", 56 id: "id0007", 57 life: 44, 58 title: "テスト7", 59 tag: ["tag2","tag7"], 60 description: "sub text" 61 } 62 ]; 63 64 /*================================================================ 65 スクリプトはじまり 66 ================================================================*/ 67 function init() { 68 69 //イベント登録 70 $(".filter_life select").on("change", onFilterChange); 71 $(".filter_tag input").on("change", onFilterChange); 72 $(".filter_keyword button").on("click", onFilterChange); 73 74 75 //最初は全て出力 76 refleshHtml(allList); 77 } 78 79 /*================================================================ 80 HTML出力 81 ================================================================*/ 82 function refleshHtml(list) { 83 84 var outputHtml = ''; 85 86 //出力する内容をoutputHtmlに格納 87 if (list.length > 0) { 88 _.each(list, function(line, i) { 89 outputHtml += '<a href="' + line.link + '" class="product">'; 90 outputHtml += ' <div class="photo"><img src="images/' + line.id + '.jpg" alt="' + line.title + '" /></div>'; 91 outputHtml += ' <div class="info">'; 92 outputHtml += ' <h3 class="productTitle">' + line.title + '</h3>'; 93 /* 94 outputHtml += ' <p class="productLife">価格(税込): ' + line.life + '円</p>'; 95 */ 96 outputHtml += ' <p class="description">' + line.description + '</p>'; 97 outputHtml += ' <ul class="tag">'; 98 _.each(line.tag, function(tag, i){ 99 outputHtml += ' <li>' + tag + '</li>'; 100 }); 101 outputHtml += ' </ul>'; 102 outputHtml += ' </div>'; 103 outputHtml += '<!--/.product--></a>'; 104 }); 105 } else { 106 outputHtml += '<div class="noproduct"><p>条件に当てはまる施工事例を検索できませんでした。</p></div>'; 107 } 108 109 //HTML出力(フェードインアニメーションつき) 110 $('.productArea').html(outputHtml); 111 $('.productArea .product').css({opacity: 0}).each(function(i){$(this).delay(100 * i).animate({opacity:1}, 300); 112 }); 113 114 //検索件数表示 115 $('.productCntArea').html('<p>' + allList.length + '件中 ' + list.length + '件を表示</p>'); 116 } 117 118 /*================================================================ 119 絞り込み条件を変更した時 120 ================================================================*/ 121 function onFilterChange(e) { 122 123 var filterFncs = []; 124 var result = []; 125 126 //セレクトボックスの値を引数に指定した関数filterByLifeをfilterFuncs配列に格納 127 filterFncs.push( 128 function(list) { 129 return filterByLife(list, $('.filter_life select').val()); 130 } 131 ); 132 133 //チェックボックスの値を引数に指定した関数filterByTagをfilterFuncs配列に格納 134 filterFncs.push( 135 function(list) { 136 return filterByTag(list, $('.filter_tag input:checked')); 137 } 138 ); 139 140 //キーワードの値を引数に指定した関数filterByKeywordをfilterFuncs配列に格納 141 filterFncs.push( 142 function(list) { 143 return filterByKeyword(list, _.escape($('.filter_keyword input').val())); 144 } 145 ); 146 147 //FilterFuncs配列内の関数をバケツリレーみたいに1つずつ実行して結果をresult配列に格納 148 result = _.reduce(filterFncs, function(list, fnc) { 149 return fnc(list); 150 }, allList); 151 152 //絞り込んだ結果を出力 153 refleshHtml(result); 154 } 155 156 /*================================================================ 157 絞り込み[1] セレクトボックス絞り込み関数 158 ================================================================*/ 159 function filterByLife(list, value) { 160 161 //絞り込み指定がない場合はリターン 162 if (value == "") { 163 return list; 164 } 165 166 //選択したセレクトボックスとlifeがマッチするかでフィルタリング 167 return _.filter(list, function(item) { 168 switch (value) { 169 /* 170 case '1': 171 return item.life <= 1; 172 */ 173 174 case '1': 175 return 1 < item.life && item.life <= 1000; 176 case '2': 177 return 1001 < item.life && item.life <= 2000; 178 case '3': 179 return 2001 < item.life && item.life <= 3000; 180 } 181 }); 182 } 183 184 /*================================================================ 185 絞り込み[2] チェックボックス絞り込み関数 186 ================================================================*/ 187 function filterByTag(list, value) { 188 189 //絞り込み指定がない場合はリターン 190 if (value.length == 0) { 191 return list; 192 } 193 194 //選択した属性(チェックボックス)とtagがマッチするかでフィルタリング 195 return _.filter(list, function(item) { 196 197 var isMatch = false; 198 199 //配列同士の比較 200 _.each(value, function(chkItem, i) { 201 202 _.each(item.tag, function(tagItem, i) { 203 if (tagItem === $(chkItem).val()) { 204 isMatch = true; 205 } 206 }); 207 208 }); 209 210 return isMatch; 211 }); 212 } 213 214 /*================================================================ 215 絞り込み[3] テキストボックス絞り込み関数 216 ================================================================*/ 217 function filterByKeyword(list, value) { 218 219 //絞り込み指定がない場合はリターン 220 if (value == "") { 221 return list; 222 } 223 224 //検索キーワードを配列に格納(スペースがある場合は複数格納) 225 var freeAry = []; 226 var val = value.replace(/ /g, " "); 227 searchAry = val.split(" "); 228 229 //入力したキーワードがtitleもしくdescriptionにマッチするかでフィルタリング 230 return _.filter(list, function(item) { 231 232 var isMatch = false; 233 234 _.each(searchAry, function(data, i) { 235 if (item.title.indexOf(data) != -1 || item.description.indexOf(data) != -1) { 236 isMatch = true; 237 } 238 }); 239 240 return isMatch; 241 242 }); 243 244 } 245 246 /*================================================================ 247 スクリプトはじめ 248 ================================================================*/ 249 init(); 250 251});
試したこと
以下のHTMLおよびjs上のselectをinputに変更してみました。
html
1<!--※変更前--> 2<select name="life" size="5"> 3 <option value="">ALL</option> 4 <option value="1">TYPE</option> 5 <option value="2">SHOP</option> 6 <option value="3">OFFICE</option> 7</select> 8 9<!--※変更後--> 10//【selectからinputに】 11<label for="ra01"> 12 <input type="radio" name="life" value="" id="ra01">ALL 13</label> 14<label for="ra02"> 15 <input type="radio" name="life" value="1" id="ra02">TYPE 16</label for="chk01"> 17<label for="ra03"> 18 <input type="radio" name="life" value="2" id="ra03">SHOP 19</label> 20<label for="ra04"> 21 <input type="radio" name="life" value="3" id="ra04">OFFICE 22</label>
js
1<!--※変更前--> 2function init() { 3 //イベント登録 4 $(".filter_life select").on("change", onFilterChange); 5・ 6・ 7・ 8 9<!--※変更後--> 10function init() { 11 //イベント登録 12 //【".filter_life select"】を以下に 13 $(".filter_life input").on("change", onFilterChange); 14・ 15・ 16・ 17 18 19<!--※変更前--> 20/*================================================================ 21絞り込み条件を変更した時 22================================================================*/ 23function onFilterChange(e) { 24 var filterFncs = []; 25 var result = []; 26 27 //セレクトボックスの値を引数に指定した関数filterByLifeをfilterFuncs配列に格納 28 filterFncs.push( 29 function(list) { 30 return filterByLife(list, $('.filter_life select').val()); 31 } 32 ); 33・ 34・ 35・ 36 37<!--※変更後--> 38/*================================================================ 39絞り込み条件を変更した時 40================================================================*/ 41function onFilterChange(e) { 42 43 var filterFncs = []; 44 var result = []; 45 46 //セレクトボックスの値を引数に指定した関数filterByLifeをfilterFuncs配列に格納 47 filterFncs.push( 48 function(list) { 49 //【return filterByLife(list, $('.filter_life select').val());】から変更 50 return filterByLife(list, $('.filter_life input').val()); 51 } 52 ); 53・ 54・ 55・
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/22 12:09
2020/05/22 13:04
2020/05/22 13:07
2020/05/22 13:17
2020/05/22 13:18