简体   繁体   English

根据优先选择从特定的JSON对象获取价值

[英]Get value from specific JSON object based on first selection

I have a set of dropdowns which I will be populating with JSON. 我有一组下拉列表,其中将填充JSON。 What I would like to do is populate the first dropdown with the name of each object. 我想做的是用每个对象的名称填充第一个下拉列表。 Then when someone selects an option from the first dropdown, the second dropdown is populated with other data pertaining to their first selection. 然后,当某人从第一个下拉列表中选择一个选项时,第二个下拉列表中将填充与他们的第一个选择相关的其他数据。 What I have right now is populating the first select box with the name of a product and assigning the value to the ID For the second dropdown I want the options to be based on the options associated with the product that was selected in the first dropdown, based on ID. 我现在所拥有的就是用产品名称填充第一个选择框,并将值分配给ID对于第二个下拉菜单,我希望这些选项基于与第一个下拉菜单中选择的产品相关的选项,根据ID。

Here's what I have so far, I am not sure how to only get the data from the selected product based on the products ID: 到目前为止,这是我所不确定的,我不确定如何仅基于产品ID从所选产品中获取数据:

 $(function() { var products = [{ "id": 0, "name": "First Product", "sizes": [{ "size": "small" },{ "size": "medium" }] }, { "id": 1, "name": "Second Product", "sizes": [{ "size": "small" },{ "size": "medium" }] } ] $.each(products, function(key, val) { $('.first').append('<option value="' + val.id + '">' + val.name + '</option>'); }); $('.first').change(function() { var selectedProduct = $(this).val(); $.each(products['id'][selectedProduct], function(key, val) { $('second').append('<option id="' + val.sizes.size + '">' + val.sizes.size + '</option>'); }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="first"> </select> <select class="second"> </select> 

You just need to iterate over products again to find the one with the currently selected ID. 您只需要再次遍历产品,以查找具有当前所选ID的产品。 Then, iterate over its sizes and fill the second just like you did with the first. 然后,迭代其大小,然后像填充第一个一样填充第二个。

Clear the options each time. 每次清除选项。

And, trigger a 'change' at the start to get the second's filled to begin with. 并且,在开始时触发“更改”,以开始填充第二个。

$(function() {
  var products = [{
        "id": 0,
        "name": "First Product",
        "sizes": [{
          "size": "small"
        },{
          "size": "medium"
        }]
      },
      {
        "id": 1,
        "name": "Second Product",
        "sizes": [{
          "size": "med"
        },{
          "size": "large"
      }]
    }
  ]

    $.each(products, function(key, val) {
      $('.first').append('<option value="' + val.id + '">' + val.name + '</option>');
    });

    $('.first').change(function() {
      var selectedProduct = $(this).val();
      $.each(products, function(key, val) {
          if (val.id==selectedProduct) {
              $('.second').find('option').remove();
              $.each(val.sizes, function(key2, val2) {
                  $('.second').append('<option value="' + val2.size + '">' + val2.size + '</option>');
              });
          }
      });
    });

    $('.first').trigger('change');
});

You could stick the object on the option as a data field for reference. 您可以将对象粘贴在选项上作为数据字段以供参考。

 $(function() { var products = [{ "id": 0, "name": "First Product", "sizes": [{ "size": "small" },{ "size": "medium" }] }, { "id": 1, "name": "Second Product", "sizes": [{ "size": "small" },{ "size": "medium" }] } ] $.each(products, function(key, val) { var $option = $('<option>').val(val.id).text(val.name).data('object-reference', val); $('.first').append($option); }); $('.first').change(function() { var selectedProduct = $(this).find('option:selected'); $.each(selectedProduct.data('object-reference').sizes, function(key, val) { $('.second').append('<option id="' + val.size + '">' + val.size + '</option>'); }); }); setTimeout(function(){ products[1].sizes = [ { 'size': 'changed1' }, { 'size': 'changed2' } ]; }, 2000); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="first"> </select> <select class="second"> </select> 

$(function() {
    var products = [{
        "id": 0,
        "name": "First Product",
        "sizes": [{
            "size": "small"
        },{
            "size": "medium"
        }]
    }, {
        "id": 1,
        "name": "Second Product",
        "sizes": [{
            "size": "small"
        },{
            "size": "medium"
        }]
    }]

    $.each(products, function(key, val) {
        $('.first').append('<option value="' + val.id + '">' + val.name + '</option>');
    });

    $('.first').change(function() {
        var selectedProduct = $(this).val();

        var getProduct = products.filter(function( obj ) {
            return obj.id == selectedProduct;
        });
        $('.second').find('option').remove();
        $.each(getProduct[0].sizes, function(key, val) {
               $('.second').append('<option id="' + val.size + '">' + val.size + '</option>');
             });
    });
});

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

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