【问题标题】:Nested Json Object display in different dropdownlist using jquery使用 jquery 在不同的下拉列表中显示嵌套的 Json 对象
【发布时间】:2014-09-18 05:19:06
【问题描述】:

从下拉列表中选择 JSON 对象时,与该对象相关的值应显示在正文或文本框中。

下拉列表控件可以是静态的或动态的。

假设 如果我在下拉列表中选择“BFS-Retails”,那么在另一个下拉列表中应该显示“Industries”、“CompaniesImpacted”、“AverageVolatility”、“Others”。当我选择“其他”时 然后在另一个下拉列表中应显示“FinancialIndustries”、“RegulatoryIndustries”、“MAIndustries”、“RestructuringIndustries”、“LeadershipIndustries”。当我选择“行业”时 它应该在某些文本框或正文内容中以及所有其他 JSON 对象中显示完整的值。

任何人都可以使用 jquery 帮助我解决这些问题吗 下面是我的 json 演示。

    {
   "BFS-Retail":{
      "Industries":{
         "A":100,
         "B":50.8292245629763,
         "C":81.5777678171224
      },
      "CompaniesImpacted":{
         "A":62.1621621621622,
         "B":48.6486486486487,
         "C":70.2702702702703
      },
      "AverageVolatility":{
         "A":2.62162162162162,
         "B":1.7027027027027,
         "C":1.89189189189189
      },
      "Others":{
         "FinancialIndustries":{
            "A":0.200430812566127,
            "B":0.189938259829807,
            "C":0.157663896336683
         },
         "RegulatoryIndustries":{
            "A":0.296020892405356,
            "B":0.114314693416088,
            "C":0.218004399872945
         },
         "MAIndustries":{
            "A":0.493368154008927,
            "B":0.233905449605226,
            "C":0.490509899714126
         },
         "RestructuringIndustries":{
            "A":0.140301568796289,
            "B":0.0439671897754184,
            "C":0.163503299904709
         },
         "LeadershipIndustries":{
            "A":0.499535255934039,
            "B":0.246216262742343,
            "C":0.246216262742343
         }
      }
   },
   "BFS-Commercial":{
      "Industries":{
         "A":1.38065889735545,
         "B":0.30681308830121,
         "C":27.9199910354101
      },
      "CompaniesImpacted":{
         "A":15,
         "B":5,
         "C":35
      },
      "AverageVolatility":{
         "A":0.15,
         "B":0.1,
         "C":1.3
      },
      "Others":{
         "FinancialIndustries":{
            "A":0,
            "B":0,
            "C":0.0206818181818182
         },
         "RegulatoryIndustries":{
            "A":0,
            "B":0,
            "C":0.0206818181818182
         },
         "MAIndustries":{
            "A":0.0045,
            "B":0.0025,
            "C":0.144772727272727
         },
         "RestructuringIndustries":{
            "A":0,
            "B":0.0025,
            "C":0.124090909090909
         },
         "LeadershipIndustries":{
            "A":0.018,
            "B":0,
            "C":0.144772727272727
         }
      }
   }
}

【问题讨论】:

  • 到目前为止你发现了什么?你到底卡在哪里了?
  • 你是如何从 json 对象创建下拉列表的?
  • 您需要根据下拉菜单中的级别动态创建下拉菜单。
  • @felix 嗨,我想使用 2 或 3 个下拉列表实现显示这些嵌套的 JSON 对象。根据选定的项目,我想显示 json 值
  • 我明白这一点。但是您究竟遇到了什么问题?你被困在哪里了?我的意思是,你一定尝试过某事。你知道如何解析 JSON 吗?你知道如何迭代对象的属性吗?如何使用递归?如何创建 DOM 元素?如何用<option>s 填充<select> 元素?如何绑定事件处理程序?这个问题有很多部分,你必须告诉我们你到底遇到了什么问题。

标签: jquery


【解决方案1】:

这是一个基本的动态选项生成代码,同样的过程也可以用来生成其他选择的下拉列表

var firstSelect = $('<select id="firstSelect"></select>');
var secondSelect = $('<select id="secondSelect"> </select>');
$.each(data, function(item, key) {
    firstSelect.append('<option >' + item + '</option>');
});
$("#container").html(firstSelect);
$("#firstSelect").on("change", function(e) {
    var item;
    var selected = $(this).val();
    if (selected === "BFS-Retail") {
        item = data[selected];
    } else {
        item = data[selected];
    }
    $(secondSelect).html('');
    $.each(item, function(item, key) {
        secondSelect.append('<option >' + item + '</option>');
    });
});

$("#container").append(secondSelect);

小提琴链接:: http://jsfiddle.net/036easd8/1/

【讨论】:

  • 谢谢 Surz,你能告诉我如果我从第二个下拉列表中选择“其他”,那么在第三个下拉列表中应该显示“FinancialIndustries”、“RegulatoryIndustries”等。以及如何显示相关值到选定的对象。
猜你喜欢
  • 2020-07-25
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-24
  • 2011-09-29
  • 1970-01-01
相关资源
最近更新 更多