简体   繁体   English

如何存储 json 下拉列表中的项目

[英]how to store items from a json dropdown list

I am trying to select store or validate multiple items from a dropdown list but I don't how to do it.我正在尝试 select 存储或验证下拉列表中的多个项目,但我不知道该怎么做。 let me explain what I am trying to do... I have food quality into a json list让我解释一下我要做什么...我将食品质量纳入 json 列表

   <select id="locality-dropdown" name="locality" multiple="multiple" size="2">
    </select>

<script>
    var Food = //4 elements json format 
        [
           {
              "name": "food1",
              "Glc": 2,
              "Lip": 0.2,
              "Prot": 0.5,
              "IG": 20
            },
           {
              "name": "food2",
               "Glc": 4,
              "Lip": 1.2,
              "Prot": 0.7,
              "IG": 40
           },
           {
              "name": "food3",
              "Glc": 5,
              "Lip": 0.32,
              "Prot": 0.76,
              "IG": 60
           },
           {
              "name": "food4",
               "Glc": 7.5,
              "Lip": 1.5,
              "Prot": 1.3,
              "IG": 80
           }       
        ];

then I put this list in a dropdown format:然后我把这个列表放在一个下拉格式中:

    let dropdown = document.getElementById('locality-dropdown');
    dropdown.length = 0;
    let defaultOption = document.createElement('option');
    defaultOption.text = 'Choose Food';
    
    dropdown.add(defaultOption);
    dropdown.selectedIndex = 0;
    
        let option;
        for (let i = 0; i<Food.length; i++) {
          option = document.createElement('option');
          option.text = Food[i].name;
          option.value0 = Food[i].Glc;
          option.value1 = Food[i].Lip;
          option.value2 = Food[i].Prot;
          option.value3 = Food[i].IG;
          console.log(option.value0,option.value1,option.value2,option.value3)
          dropdown.add(option);}
</script>

At that point I have a nice dropdown list box with food1, food2, food3, food4 inside.那时我有一个很好的下拉列表框,里面有 food1、food2、food3、food4。

voila?瞧? and now what to do ?!现在该怎么办?!

I need to select some of them and from these selected items, use the numbers of the list shown above to follow my math such as sum of Glc, sum of Lip etc.我需要 select 其中一些,并从这些选定的项目中,使用上面显示的列表的数字来遵循我的数学运算,例如 Glc 的总和、Lip 的总和等。

I don't know if I am clear enough but let say I choose food1 and food3, then because I am choosing them, it will return later, when needed: Glc of food1 + Glc of food3;我不知道我是否足够清楚,但是假设我选择了food1和food3,那么因为我正在选择它们,所以稍后会在需要时返回:food1的Glc + food3的Glc; Lip of food1 + Lip of food3 etc - if 3 elements are selected so the same process with 3 elements. food1 的唇 + food3 的唇等 - 如果选择了 3 个元素,则使用 3 个元素进行相同的处理。 I don't know how to write the code and which technic to use whether an other json, an array of the the selected elements to do so...?我不知道如何编写代码以及使用哪种技术是否另一个 json,所选元素的数组这样做......?

Try to make a checkbox list instead of a dropdown list:尝试制作一个复选框列表而不是下拉列表:

 var Food = //4 elements json format [{ "name": "food1", "Glc": 2, "Lip": 0.2, "Prot": 0.5, "IG": 20 }, { "name": "food2", "Glc": 4, "Lip": 1.2, "Prot": 0.7, "IG": 40 }, { "name": "food3", "Glc": 5, "Lip": 0.32, "Prot": 0.76, "IG": 60 }, { "name": "food4", "Glc": 7.5, "Lip": 1.5, "Prot": 1.3, "IG": 80 } ]; for (var i=0;i<Food.length;i++){ CurrentFood=Food[i] var input=document.createElement("input") input.type="checkbox" input.id=CurrentFood.name var label=document.createElement("label") label.for=CurrentFood.name label.innerText=CurrentFood.name document.body.appendChild(input) document.body.appendChild(label) document.body.appendChild(document.createElement("br")) }

After that, attach an event listener to each checkbox listening for the "change" event.之后,将事件侦听器附加到每个复选框以侦听“更改”事件。 Checkbox Check Event Listener After that, whenever the event fires, find the index from the parent of the ones that are checked, get the indexed arrays from the list, and return Food[index].Glc or Food[index].Lip , or whatever you want. Checkbox Check Event Listener之后,每当事件触发时,从被检查的那些的父级中找到索引,从列表中获取索引的 arrays,并返回Food[index].GlcFood[index].Lip ,或无论你想要什么。

You have a few options with regard to how you can store the individual data elements corresponding to the list elements in the options value attribute.关于如何在选项值属性中存储与列表元素相对应的单个数据元素,您有几个选项。

  1. Use array indices.使用数组索引。
  2. Transform your data into an object keyed on the name and look it up from that.将您的数据转换为键入名称的 object 并从中查找。
  3. Convert the data to json and store it in the value attribute.将数据转换为 json 并存储在 value 属性中。 Note: This method would mean that if you updated your Food array after the dropdown was built, then you wouldn't see the updates in the options value attributes.注意:此方法意味着如果您在构建下拉菜单后更新了Food数组,那么您将不会在选项value属性中看到更新。 If it needs to update individual properties, don't use this method.如果需要更新个别属性,请不要使用此方法。

Here I've done it using json.在这里,我使用 json 完成了它。 Its trivial to change it to the other two.将其更改为其他两个是微不足道的。

<html>
    <body>
        <select id="locality-dropdown" name="locality" multiple="multiple">
        </select>
        <button id="locality-but">Submit</button>
    </body>

<script>
    "use strict"
    const Food = [{
          "name": "food1",
          "Glc": 2,
          "Lip": 0.2,
          "Prot": 0.5,
          "IG": 20
        }, {
          "name": "food2",
           "Glc": 4,
          "Lip": 1.2,
          "Prot": 0.7,
          "IG": 40
       }, {
          "name": "food3",
          "Glc": 5,
          "Lip": 0.32,
          "Prot": 0.76,
          "IG": 60
       }, {
          "name": "food4",
          "Glc": 7.5,
          "Lip": 1.5,
          "Prot": 1.3,
          "IG": 80
       }       
    ];

    const dropdown = document.getElementById('locality-dropdown');
    const defaultOption = document.createElement('option');
    defaultOption.text = 'Choose Food';
    defaultOption.disabled = true;
    
    dropdown.add(defaultOption);
    dropdown.selectedIndex = 0;
    
    document.getElementById('locality-but').addEventListener('click', (event) => {
        console.log('click');
        const dd = document.getElementById('locality-dropdown');
        const result = [];
        for(let i = 0; i < dd.options.length; i++) {
            const option = dd.options[i];
            
            if(option.selected) {
                result.push(JSON.parse(option.value));
            }            
        }
        console.log("do something with \n", result);
    })
    
    for (let food of Food) {
        const option = document.createElement('option');
        option.text = food.name;
        option.value = JSON.stringify(food);
        dropdown.add(option);
    }
</script>
    
</html>

At the end of the event listener you have an array called result which has the data for all selected options.在事件侦听器的末尾,您有一个名为result的数组,其中包含所有选定选项的数据。 You should be able to adapt this to fit your needs.您应该能够对其进行调整以满足您的需求。

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

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