根据选择填充下拉菜单

时间:2019-07-22 06:16:23

标签: javascript jquery html

我有一个“年份”选择,一个“品牌”选择,我只想填充与他们拥有的年份相关的品牌。我目前可以从fitmentData.json填充所有数据,但是当我尝试过滤选择内容时,我想不出一种使它起作用的方法。目前,我有一个我认为可以正常运行的updateMake()函数,但是在黑客入侵了大约2个小时之后,我找不到解决方案。

html

  <select name="year" id="year">

  </select>

  <select id="make" name="make">

  </select>
  <select id="model" name="model">

  </select>

js

 //year
    let yearDropdown = $('#year');
    yearDropdown.append('<option selected="true" disabled>Choose     
Year</option>');

    //make(brand in json)
    let brandDropdown = $('#make');
    brandDropdown.append('<option selected="true" disabled>Choose  
Make</option>');

    const url = 'fitmentData.json';

    //append json years in dropdown
    $.getJSON(url, function (data) {
      $.each(data, function (key, entry) {
        yearDropdown.append($('<option></option>').attr('value',  
entry.year).text(entry.year));
      })
      //remove year duplicates
      var map = {}
      $('select option').each(function () {
        if (map[this.value]) {
          $(this).remove()
        }
        map[this.value] = true;
      })
      //append json makes in dropdown
      $.each(data, function (key, entry) {
        brandDropdown.append($('<option></option>').attr('value',  
entry.brand).text(entry.brand));
        updateMake();
      });
      //remove make duplicates
      var map = {}
      $('select option').each(function () {
        if (map[this.value]) {
          $(this).remove()
        }
        map[this.value] = true;
      })

    //updates makes depending on selection
    function updateMake() {
      brandDropdown.empty();
      brandDropdown.append('<option selected="true" disabled>Choose  
Make</option>');
      if (yearDropdown.value == '1976') {
        var optionArray = ["Yamaha", "Kawasaki"];
        //populate makes depending on year
        for (i = 0; i <= optionArray.length; i++) {
          var newOption = document.createElement('option');
          newOption.innerHTML = optionArray[i];
          brandDropdown.append(newOption);
        }
      }
    }

fitmentData.json

 [
 {
    "brand": "Yamaha",
    "model": "XT500",
    "year": "1976"
  },
  {
    "brand": "Honda",
    "model": "TL250",
    "year": "1976"
  },
  {
    "brand": "Yamaha",
    "model": "TT500",
    "year": "1976"
  },
  {
    "brand": "Honda",
    "model": "XL125",
    "year": "1976"
  },
  {
    "brand": "Kawasaki",
    "model": "KE125",
    "year": "1976"
  },
  {
    "brand": "Yamaha",
    "model": "YZ175",
    "year": "1976"
  },
  {
    "brand": "Honda",
    "model": "TL125",
    "year": "1976"
  },
  {
    "brand": "Suzuki",
    "model": "RM125",
    "year": "1976"
  }
]

再一次,我能够在下拉列表中填充所有json数据,但updateMake()函数无法按预期工作。 updateMake()使make完全不填充任何数据。

1 个答案:

答案 0 :(得分:3)

您可以在onchange元素上添加year事件,以便在发生更改时调用updateMake()。另外,由于yearDropdown.value是jQuery对象,因此yearDropdown也不起作用,请考虑改用yearDropdown.val()

var data = [{
    "brand": "Yamaha",
    "model": "XT500",
    "year": "1976"
  },
  {
    "brand": "Honda",
    "model": "TL250",
    "year": "1976"
  },
  {
    "brand": "Yamaha",
    "model": "TT500",
    "year": "1976"
  },
  {
    "brand": "Honda",
    "model": "XL125",
    "year": "1976"
  },
  {
    "brand": "Kawasaki",
    "model": "KE125",
    "year": "1976"
  },
  {
    "brand": "Yamaha",
    "model": "YZ175",
    "year": "1976"
  },
  {
    "brand": "Honda",
    "model": "TL125",
    "year": "1976"
  },
  {
    "brand": "Suzuki",
    "model": "RM125",
    "year": "1976"
  }
];



//year
let yearDropdown = $('#year').append('<option selected="true" disabled>Choose Year</option>').on('change', function() {
  updateMake();
});

//make(brand in json)
let brandDropdown = $('#make').append('<option selected="true" disabled>Choose Make</option>');

//append json years in dropdown
$.each(data, function(key, entry) {
  yearDropdown.append($('<option></option>').attr('value', entry.year).text(entry.year));
})
//remove year duplicates
var map = {}
$('select option').each(function() {
  if (map[this.value]) {
    $(this).remove()
  }
  map[this.value] = true;
})
//append json makes in dropdown
$.each(data, function(key, entry) {
  brandDropdown.append($('<option></option>').attr('value', entry.brand).text(entry.brand));
  updateMake();
});
//remove make duplicates
var map = {}
$('select option').each(function() {
  if (map[this.value]) {
    $(this).remove()
  }
  map[this.value] = true;
})

//updates makes depending on selection
function updateMake() {
  brandDropdown.empty();
  brandDropdown.append('<option selected="true" disabled>Choose Make</option>');
  if (yearDropdown.val() == '1976') {
    var optionArray = ["Yamaha", "Kawasaki"];
    //populate makes depending on year
    for (i = 0; i < optionArray.length; i++) {
      var newOption = document.createElement('option');
      newOption.innerHTML = optionArray[i];
      brandDropdown.append(newOption);
    }
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="year" id="year">

</select>

<select id="make" name="make">

</select>
<select id="model" name="model">

</select>