简体   繁体   English

如何在 HTML Javascript 中填充第三个下拉列表?

[英]How to Populate the 3rd Dropdown in HTML Javascript?

I'm having a problem not knowing how to populate or create the 3rd box that will show the result on the 2nd box.我有一个问题,不知道如何填充或创建将在第二个框中显示结果的第三个框。 What I'm trying to do is select the brand, then model then on 3rd box, it will show their model numbers.我要做的是选择品牌,然后选择型号,然后在第三个框上,它会显示他们的型号。

<select name="device" id="device" onchange="changecat(this.value);">
  <option value="" disabled selected>Select</option>
  <option value="A">Apple</option>
  <option value="S">Samsung</option>
  <option value="L">LG</option>
  <option value="G">Google</option>
  </select>
  <select name="category" id="category">
    <option value="" disabled selected>Select</option>
  </select>

and here's the other code这是另一个代码

var brandByCategory = {
  A: ["iPhone", "iPhone 3G", "iPhone 3GS", "iPhone 4"],
  S: ["Galaxy S3", "Galaxy S4", "Galaxy S5", "Galaxy S6"],
  L: ["G3", "G4", "G5", "G6", "G7"],
  G: ["Pixel", "Pixel XL", "Pixel 2", "Pixel 2 XL", "Pixel 3", "Pixel 3 XL", "Pixel 3A", "Pixel 3A XL", "Pixel 4", "Pixel 4 XL"]

}

    function changecat(value) {
        if (value.length == 0) document.getElementById("category").innerHTML = "<option></option>";
        else {
            var catOptions = "";
            for (categoryId in brandByCategory[value]) {
                catOptions += "<option>" + brandByCategory[value][categoryId] + "</option>";
            }
            document.getElementById("category").innerHTML = catOptions;
        }
    }

If you want to save the model number with the device, I'd suggest you to use array of objects and add the model number as value to second drop down as shown below.如果您想用设备保存型号,我建议您使用对象数组并将型号作为值添加到第二个下拉列表中,如下所示。

And from second dropdown, you can get the value and then display it wherever you want.从第二个下拉菜单中,您可以获得该值,然后将其显示在您想要的任何位置。

 var brandByCategory = { A: [{ name: "iPhone", model: "10" }, { name: "iPhone 3G", model: "3G" }], S: [{ name: "Galaxy S3", model: "S3" }, { name: "Galaxy S4", model: "S4" }] } function changecat(value) { if (value.length == 0) document.getElementById("category").innerHTML = "<option></option>"; else { var catOptions = ""; for (categoryId in brandByCategory[value]) { catOptions += `<option value = "${brandByCategory[value][categoryId].model}">${brandByCategory[value][categoryId].name}</option>`; } document.getElementById("category").innerHTML = catOptions; } } var input = document.getElementById("modelNumber") function populateValue(val) { input.value = val; }
 <select name="device" id="device" onchange="changecat(this.value);"> <option value="" disabled selected>Select</option> <option value="A">Apple</option> <option value="S">Samsung</option> </select> <select name="category" id="category" onchange="populateValue(this.value);"> <option value="" disabled selected>Select</option> </select> <input type="text" id="modelNumber" />

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

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