Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I prefer data structure like this:</p> <pre><code>var carMakers = [ { name: 'Honda', models: [ { name: 'Accord', features: ['2dr', '4dr'] }, { name: 'CRV', features: ['2dr', 'Hatchback'] }, { name: 'Pilot', features: ['base', 'superDuper'] } ]}, { name: 'Toyota', models: [ { name: 'Prius', features: ['green', 'superGreen'] }, { name: 'Camry', features: ['sporty', 'square'] }, { name: 'Corolla', features: ['cheap', 'superFly'] } ]} ]; </code></pre> <p>Given the three select lists with id's: 'maker', 'model' and 'features' you can manipulate them with this (I believe this is pretty self explanatory):</p> <pre><code>// returns array of elements whose 'prop' property is 'value' function filterByProperty(arr, prop, value) { return $.grep(arr, function (item) { return item[prop] == value }); } // populates select list from array of items given as objects: { name: 'text', value: 'value' } function populateSelect(el, items) { el.options.length = 0; if (items.length &gt; 0) el.options[0] = new Option('please select', ''); $.each(items, function () { el.options[el.options.length] = new Option(this.name, this.value); }); } // initialization $(document).ready(function () { // populating 1st select list populateSelect($('#maker').get(0), $.map(carMakers, function(maker) { return { name: maker.name, value: maker.name} })); // populating 2nd select list $('#maker').bind('change', function() { var makerName = this.value, carMaker = filterByProperty(carMakers, 'name', makerName), models = []; if (carMaker.length &gt; 0) models = $.map(carMaker[0].models, function(model) { return { name: model.name, value: makerName + '.' + model.name} }); populateSelect($('#model').get(0), models); $('#model').trigger('change'); }); // populating 3rd select list $('#model').bind('change', function () { var nameAndModel = this.value.split('.'), features = []; if (2 == nameAndModel.length) { var makerName = nameAndModel[0], carModel = nameAndModel[1], carMaker = filterByProperty(carMakers, 'name', makerName); if (carMaker.length &gt; 0) { var model = filterByProperty(carMaker[0].models, 'name', carModel) if (model.length &gt; 0) features = $.map(model[0].features, function(feature) { return { name: feature, value: makerName + '.' + carModel + '.' + feature} }) } } populateSelect($('#feature').get(0), features); }) // alerting value on 3rd select list change $('#feature').bind('change', function () { if (this.value.length &gt; 0) alert(this.value); }) }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload