简体   繁体   中英

JavaScript jQuery dependent drop down list

I have this dependent drop down box code, in which you pick first selection (motherboard) and the other ones(ram and video board) will show up depending on the first pick.

 $(function(){ var $cat = $("#motherboard"), $subcat = $(".subcat"); $cat.on("change",function(){ var _rel = $(this).val(); $subcat.find("option").attr("style",""); $subcat.val(""); if(!_rel) return $subcat.prop("disabled",true); $subcat.find("[rel~='"+_rel+"']").show(); $subcat.prop("disabled",false); }); }); 
 .subcat option{ display:none; } .subcat option.label{ display:block; } /* useless template */ div{ margin-bottom:10px; float:left; width:100%; } label{ float:left; width:200px; margin-left:20px } p{ padding:5px 20px; background-color:#f1f1f1; float:left; border:solid 1px #ccc; color:#333; text-transform:uppercase; margin: 20px 0 30px; } body{ font-family:arial; font-size:14px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h1>Made your PC:</h1> <div> <label>Select motherboard: </label><select id="motherboard" name="motherboard"> <option class="label" value>Select Motherboard</option> <!-- Home Ware --> <option value="AS1">ASUS RAMPAGE V EXTREME</option> <option value="AS2">ASUS ATX DDR3 2600 LGA</option> <option value="GB1">Gigabyte AM3+</option> <option value="MSI1">MSI ATX DDR3 2600 LGA 1150</option> </select> </div> <p><strong>Only compatible components will show.</strong></p> <div> <label>Select RAM: </label> <select disabled="disabled" class="subcat" id="RAM" name="RAM"> <option class="label" value>RAM Memory</option> <option rel="AS1 AS2 GB1" value="KI1">Kingston Value RAM</option> <option rel="AS1 AS2 MSI1" value="P5KPL">P5KPL-AM SE</option> <option rel="MSI1 GB1" value="960GM">960GM-VGS3 FX </option> </select> </div> <div> <label>Select Video Board: </label> <select disabled="disabled" class="subcat" id="video-card" name="video-card"> <option class="label" value>Video Card</option> <option rel="MSI1 AS2" value="EVGA8400">EVGA GeForce 8400 GS</option> <option rel="AS1" value="XFXAMD">XFX AMD Radeon HD 5450</option> <option rel="MSI1 GB1" value="GTX750Ti">EVGA GeForce GTX 750Ti SC</option> </select> </div> 

You can also see it on this jsfiddle

How can I add another first option, lets say "processor", that motherboard will depend on, and then everything else just like it is, depends on motherboard.

Now its : Motherboad--->ram, video board. How to make: Processor--->motherboard--->ram, video board.

You can do achieve this by:

  • Adding a new section for the processor dropdown
  • Adding a rel attribute to the motherboards, that will contain the processor IDs
  • Indicate that the motherboards are now a subcategory (by adding class="subcat" )
  • Duplicating the onchange code for the motherboards, but applying it to the processor and targeting the motherboard instead of the subcategories

The code would be like:

 $(function(){ // add a supercategory for the processor var $supcat = $("#processor"), $cat = $("#motherboard"), $subcat = $(".subcat"); // duplicate the code for the processor -> motherboard $supcat.on("change",function(){ var _rel = $(this).val(); $cat.find("option").attr("style",""); $cat.val(""); if(!_rel) return $cat.prop("disabled",true); $cat.find("[rel~='"+_rel+"']").show(); $cat.prop("disabled",false); }); $cat.on("change",function(){ var _rel = $(this).val(); $subcat.find("option").attr("style",""); $subcat.val(""); if(!_rel) return $subcat.prop("disabled",true); $subcat.find("[rel~='"+_rel+"']").show(); $subcat.prop("disabled",false); }); }); 
 .subcat option{ display:none; } .subcat option.label{ display:block; } /* useless template */ div{ margin-bottom:10px; float:left; width:100%; } label{ float:left; width:200px; margin-left:20px } p{ padding:5px 20px; background-color:#f1f1f1; float:left; border:solid 1px #ccc; color:#333; text-transform:uppercase; margin: 20px 0 30px; } body{ font-family:arial; font-size:14px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <h1>Made your PC:</h1> <div> <label>Processeor: </label><select id="processor" name="processor"> <option class="label" value>Select Processor</option> <!-- Home Ware --> <option value="P1">Processor 1</option> <option value="P2">Processor 2</option> <option value="P3">Processor 3</option> <option value="P4">Processor 4</option> </select> </div> <p><strong>Only compatible components will show.</strong></p> <div> <label>Select motherboard: </label><select id="motherboard" name="motherboard" class="subcat" disabled="disabled"> <option class="label" value>Select Motherboard</option> <!-- Home Ware --> <option rel="P1 P2" value="AS1">ASUS RAMPAGE V EXTREME</option> <option rel="P2 P3" value="AS2">ASUS ATX DDR3 2600 LGA</option> <option rel="P1 P3 P4" value="GB1">Gigabyte AM3+</option> <option rel="P2 P4" value="MSI1">MSI ATX DDR3 2600 LGA 1150</option> </select> </div> <div> <label>Select RAM: </label> <select disabled="disabled" class="subcat" id="RAM" name="RAM"> <option class="label" value>RAM Memory</option> <option rel="AS1 AS2 GB1" value="KI1">Kingston Value RAM</option> <option rel="AS1 AS2 MSI1" value="P5KPL">P5KPL-AM SE</option> <option rel="MSI1 GB1" value="960GM">960GM-VGS3 FX </option> </select> </div> <div> <label>Select Video Board: </label> <select disabled="disabled" class="subcat" id="video-card" name="video-card"> <option class="label" value>Video Card</option> <option rel="MSI1 AS2" value="EVGA8400">EVGA GeForce 8400 GS</option> <option rel="AS1" value="XFXAMD">XFX AMD Radeon HD 5450</option> <option rel="MSI1 GB1" value="GTX750Ti">EVGA GeForce GTX 750Ti SC</option> </select> </div> 


Duplicating the code that applies to the processor ( $supcat ) and motherboard ( $cat ) is ugly. So alternatively, and to make it easier to maintain if you add new levels in the future, it could be interesting to move that code into a common function and just pass the objects that you want. Something like this:

function updateSelects(obj1, obj2) {
    var _rel = $(obj1).val();
    obj2.find("option").attr("style","");
    obj2.val("");
    if(!_rel) return obj2.prop("disabled",true);
    obj2.find("[rel~='"+_rel+"']").show();
    obj2.prop("disabled",false);
}

That would be called on the onchange event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM