简体   繁体   English

在下拉列表中使用Jquery解析JSON

[英]Parsing JSON using Jquery in dropdowns

I am building a specific location selector using dropdowns. 我正在使用下拉列表构建特定的位置选择器。 I need 3 dropdown for the same. 我需要3个下拉菜单。 Now i got the json and trying to parse it accordingly but i am having some problems. 现在,我得到了json并尝试据此进行解析,但是我遇到了一些问题。 Here is small part of json i am using. 这是我正在使用的json的一小部分。

{
"Chandigarh": {
    "CHANDIGARH": ["Sector 21 (Chandigarh)", "Sector 22 (Chandigarh)", "Sector 23 ( Chandigarh)", "Sector 26 (Chandigarh)", "Sector 29 (Chandigarh)", "Sector 30"]}
 "Andhra Pradesh": {
    "EAST GODAVARI": ["D Vemavaram", "Gangavaram", "GSL Medical College", "Konthamuru", "Navara", "Satellite City"]
      "CHITTOOR": ["Appa Rao Street", "Madanapalle Spinning Mills Gds", "Akkurthi", "Chiyyavaram", "Chodavaram", "Gottipudi", "Kasaram", "Muchivolu", "Obulayapalle"]}
}

So i am trying to parse states into one dropdown , and based on states sub-region can be selected , and based on sub-regions city name can be selected. 因此,我尝试将州解析为一个下拉列表,并根据州选择子区域,并根据子区域选择城市名称。 for eg. 例如 If i select "Andhra Pradesh" on one dropdown , other must show "East Godavri" and "chittoor" and whichever i select then must show it's cities in third dropdown. 如果我在一个下拉菜单中选择“安得拉邦”,则另一个必须显示“ East Godavri”和“ chittoor”,然后无论我选择哪个,都必须在第三个下拉列表中显示城市。 So far i have tried using jquery getjson 到目前为止,我已经尝试使用jquery getjson

$.getJSON(url, function(data) {
console.log(data);
$.each(data, function(index, value) {


  $("#state").append(
    '<option value="' + index + '">' + index + "</option>"
  );
});


 });



 $("#state").change(function() {


countryCode = $("#state").val();

$.getJSON(url, function(data) {

  $('#sub').append('<option value="">Please select your region</option>');
  $.each(data.response.venue.countryCode, function(index, value) {

    $("#sub").append(
      '<option value="' + index + '">' + index + "</option>"
    );
  });
});


 });

I am getting all the city names but not sub-regions , Anyone who can help me 我得到所有城市的名称,但没有子区域的名称,任何可以帮助我的人

First let's restruction JSON data to this for contain an option id (To use id for option's value) 首先让我们将JSON数据重构为包含选项ID(将id用作选项的值)

[
  {
    id: 1,
    name: "Chandigarh",
    regions: [
      {
        id: 2,
        name: "CHANDIGARH",
        areas: [
          { id: 3, name: "Sector 21 (Chandigarh)" },
          { id: 4, name: "Sector 22 (Chandigarh)" },
          { id: 5, name: "Sector 23 ( Chandigarh)" },
          { id: 6, name: "Sector 26 (Chandigarh)" },
          { id: 7, name: "Sector 29 (Chandigarh)" },
          { id: 8, name: "Sector 30" }
        ]
      }
    ]
  },
  {
    id: 9,
    name: "Andhra Pradesh",
    regions: [
      {
        id: 10,
        name: "EAST GODAVARI",
        areas: [
          { id: 11, name: "D Vemavaram" },
          { id: 12, name: "Gangavaram" },
          { id: 13, name: "GSL Medical College" },
          { id: 14, name: "Konthamuru" },
          { id: 15, name: "Navara" },
          { id: 16, name: "Satellite City" }
        ]
      },
      {
        id: 17,
        name: "CHITTOOR",
        areas: [
          { id: 18, name: "Appa Rao Street" },
          { id: 19, name: "Madanapalle Spinning Mills Gds" },
          { id: 20, name: "Akkurthi" },
          { id: 21, name: "Chiyyavaram" },
          { id: 22, name: "Chodavaram" },
          { id: 23, name: "Gottipudi" },
          { id: 24, name: "Kasaram" },
          { id: 25, name: "Muchivolu" },
          { id: 26, name: "Obulayapalle" }
        ]
      }
    ]
  }
]

Update the fetch code to save data to variable. 更新获取代码以将数据保存到变量。

var states;

$.getJSON(url, function (data) {
  states = data; // Save data to variable, so no need to fetch data everytime user change the select
  $("#state").html(`<option>Select State..</option>`);
  $("#region").html(`<option>Select region..</option>`);
  $("#area").html(`<option>Select area..</option>`);
  $.each(states, function (index, state) {
    $('#state').append(`<option value="${state.id}">${state.name}</option>`);
  });
});

Then assign event to select 然后分配事件以选择

$("#state").bind("change", function() { // When select state change
  let state = states.find(s => s.id == $(this).val());

  // set region options

  $("#region").html(`<option>Select region..</option>`);
  $.each(state.regions, function(index, region) {
    $("#region").append(`<option value="${region.id}">${region.name}</option>`);
  });

  $("#area").html(`<option>Select area..</option>`);
});

$("#region").bind("change", function() { // When select region change
  let region = states
    .map(s => s.regions)
    .flat()
    .find(r => r.id == $(this).val());

  // set area options

  $("#area").html(`<option>Select area..</option>`);
  $.each(region.areas, function(index, area) {
    $("#area").append(`<option value="${area.id}">${area.name}</option>`);
  });
});

Here for the full example 这里是完整的例子

https://codesandbox.io/s/5k87rkr16p https://codesandbox.io/s/5k87rkr16p

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

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