html - Reusable jQuery select/option filter based on another select -
like title suggests i'm trying create jquery function filter select options based on the value of select/option.
so far have managed create function works great single use, breaks when try use more once.
here's have far:
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'); }); }); });
i'd appreciate can on this!
thanks.
use jquery:
$(document).ready(function () { $('.model option').hide(); $('.manufacturer').change(function () { var classn = $(this).find(' option:selected').prop('class'); $(this).parent().parent().find('.model option').hide(); $(this).parent().parent().find('.model option.'+classn).show(); }); });
Comments
Post a Comment