简体   繁体   English

jquery Autofill select with options from array

[英]jquery Autofill select with options from array

I'm going to have a total of 4 dropdowns.我将总共有 4 个下拉菜单。 What I'm thinking is creating 3 arrays (first is dropdown is hardcoded), and based what's picked on in the first array, the 2nd dropdown gets populated.我在想的是创建 3 个数组(第一个下拉列表是硬编码的),并根据第一个数组中选择的内容,填充第二个下拉列表。 How do I filter out what displays based on the first selection?如何根据第一个选择过滤掉显示的内容? I already know what all the options will be for the 2nd,3rd,4th menu.我已经知道第 2、第 3、第 4 个菜单的所有选项是什么。 Here's what I'm thinking:这是我的想法:

var secondDropdown = ['Cat|c1|1','Dog|d1|1','Banana|b1|2','Apple|a1|2','Car|c2|3','Boat|b2|3'] // here's where i'm lost. How do I create an array that has 3 values. first is the display text, second is the value and third is the filter.

<select id="menu1">
<option value="1">First Option</option>
<option value="2">Second Option</option>
<option value="3">Third Option</option>
</select>

<select id="menu2"></select>

Expected result of menu2 after selecting the first dropDown would be选择第一个下拉菜单后 menu2 的预期结果将是

<select id="menu2">
    <option value="c1">Cat</option>
    <option value="d1">Dog</option>
</select

So if I pick the first option, it'll take the value and match it up with the filter in the first array and populate the 2nd dropdown.因此,如果我选择第一个选项,它将采用该值并将其与第一个数组中的过滤器匹配并填充第二个下拉列表。 Any ideas?有任何想法吗?

this can be an approach:这可以是一种方法:

 $(function() { var secondDropdown = [{ filter: "1", values: [{ value: "b1", text: "Boat" }, { value: "b2", text: "Boat2" }, { value: "b3", text: "Boat3" }] }, { filter: "2", values: [{ value: "c1", text: "Cat" }, { value: "c2", text: "Cat2" }, { value: "c3", text: "Cat3" }] }]; $('#menu1').change(function() { var currentValue = $(this).val(); $('#menu2').html(""); var content = secondDropdown.filter(function(elem) { return elem.filter == currentValue; }); $.each(content[0] != undefined ? content[0].values : [], function(i, elem) { $('#menu2').append("<option value='" + elem.value + "'>" + elem.text + "</option>"); }); }).trigger("change"); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="menu1"> <option value="1">First Option</option> <option value="2">Second Option</option> <option value="3">Third Option</option> </select> <select id="menu2"></select>

Inside your Array, use more Arrays with options values.在您的数组中,使用更多带有选项值的数组。

 var secondDropdown = [ ["Cat", "Dog", "Unicorn"], ["Banana", "Apple", "Strawberry", "Pear", "Rapsberry"], ["Car", "Boat"] ]; $("#menu1").on("change", function(){ var drop2arr = secondDropdown[this.value-1]; $("#menu2").html(function(){ return "<option>"+ (drop2arr.join("</option><option>")) +"</option>"; }); }).change();
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="menu1"> <option value="1">Animals</option> <option value="2">Fruits</option> <option value="3">Things</option> </select> <select id="menu2"></select>

This code works fine.这段代码工作正常。

var secondDropdown = ['cat|1','Dog|1','Banana|2','Apple|2','Car|3','Boat|3']; // here's where i'm lost. How do I create an array that has 2 values. first is the regular value and second value is the filter.
$("#menu1").on("change", function(){
    $("#menu2").html('');
    //Get current value of Menu1 on change event
    var value=$(this).val();
    for(var i = 0; i < secondDropdown.length; i++)
    {
        //Get associed values
        var splitted = secondDropdown[i].split('|');
        if(splitted[1] === value)
        {
            //append in the second select
            $("#menu2").append('<option value="'+splitted[1]+'">'+splitted[0]+'</option>');
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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