像標題所示,我試圖創建一個jQuery函數來根據另一個選擇/選項的值過濾選擇選項。基於另一個選擇的可重用jQuery選擇/選項過濾器
到目前爲止,我已經設法創建了一種非常適合單次使用的功能,但是當我嘗試多次使用它時會中斷。
這是我到目前爲止有:
HTML
<tr class='car-selection'>
<td class='label'>Car 1: </td>
<td>Manufacturer:
<select class="manufacturer">
<option>Select Manufacturer</option>
<option class="audi" value="Audi">Audi</option>
<option class="bmw" value="BMW">BMW</option>
<option class="mercedes-benz" value="Mercedes-Benz">Mercedes-Benz</option>
</select>
</td>
<td>Model:
<select class="model">
<option class="audi">Select Model</option>
<option value="a1" class="audi">A1</option>
<option value="a3" class="audi">A3</option>
<option value="a4" class="audi">A4</option>
<option value="a5" class="audi">A5</option>
<option value="a6" class="audi">A6</option>
<option class="bmw">Select Model</option>
<option value="1series" class="bmw">1 Series</option>
<option value="3series" class="bmw">3 Series</option>
<option value="5series" class="bmw">5 Series</option>
<option value="7series" class="bmw">7 Series</option>
<option value="x5" class="bmw">X5</option>
<option class="mercedes-benz">Select Model</option>
<option value="aclass" class="mercedes-benz">A-Class</option>
<option value="bclass" class="mercedes-benz">B-Class</option>
<option value="cclass" class="mercedes-benz">C-Class</option>
<option value="eclass" class="mercedes-benz">E-Class</option>
<option value="mclass" class="mercedes-benz">M-Class</option>
</select>
</td>
</tr>
<tr class='car-selection'>
<td class='label'>Car 2: </td>
<td>Manufacturer:
<select class="manufacturer">
<option>Select Manufacturer</option>
<option class="audi" value="Audi">Audi</option>
<option class="bmw" value="BMW">BMW</option>
<option class="mercedes-benz" value="Mercedes-Benz">Mercedes-Benz</option>
</select>
</td>
<td>Model:
<select class="model">
<option class="audi">Select Model</option>
<option value="a1" class="audi">A1</option>
<option value="a3" class="audi">A3</option>
<option value="a4" class="audi">A4</option>
<option value="a5" class="audi">A5</option>
<option value="a6" class="audi">A6</option>
<option class="bmw">Select Model</option>
<option value="1series" class="bmw">1 Series</option>
<option value="3series" class="bmw">3 Series</option>
<option value="5series" class="bmw">5 Series</option>
<option value="7series" class="bmw">7 Series</option>
<option value="x5" class="bmw">X5</option>
<option class="mercedes-benz">Select Model</option>
<option value="aclass" class="mercedes-benz">A-Class</option>
<option value="bclass" class="mercedes-benz">B-Class</option>
<option value="cclass" class="mercedes-benz">C-Class</option>
<option value="eclass" class="mercedes-benz">E-Class</option>
<option value="mclass" class="mercedes-benz">M-Class</option>
</select>
</td>
</tr>
</table>
jQuery的
$(document).ready(function() {
var allOptions = $('.model option');
$('.manufacturer').change(function() {
$('.model option').remove();
var classN = $('.manufacturer option:selected').prop('class');
var opts = allOptions.filter('.' + classN);
$.each(opts, function (i, j) {
$(j).appendTo('.model');
});
});
});
我想感謝所有幫助我能得到這個!
謝謝。
感謝輸入,但這種方法是不是跨瀏覽器兼容...我已經試過顯示/隱藏,但使用'顯示:在沒有按'option'元素」 none' t在大多數瀏覽器中工作 – Ross