<select id="_make">
<option value="">- Марка -</option>
</select>
<select id="_model">
<option value="">- Модель -</option>
</select>
<select id="_country">
<option value="">- Страна -</option>
</select>
<script>
var goods = [
{
name: {
ru:'МТЗ',
ua: 'МТЗ'
},
model: '82.1',
country: {
ru:'Беларусь',
ua: 'Білорусь'
},
price: '1.11',
images: 'img/belarus/mtz/МТЗ_82.1.jpeg'
},
{
name: {
ru:'МТЗ',
ua: 'МТЗ'
},
model: '82.1',
country: {
ru:'Россия',
ua: 'Россия'
},
price: '1.11',
images: 'img/belarus/mtz/МТЗ_82.1.jpeg'
},
{
name: {
ru:'МТЗ2',
ua: 'МТЗ2'
},
model: '82.12',
country: {
ru:'Беларусь',
ua: 'Білорусь'
},
price: '1.11',
images: 'img/belarus/mtz/МТЗ_82.1.jpeg'
},
{
name: {
ru:'МТЗ3',
ua: 'МТЗ3'
},
model: '82.13',
country: {
ru:'Беларусь',
ua: 'Білорусь'
},
price: '1.11',
images: 'img/belarus/mtz/МТЗ_82.1.jpeg'
}
];
var language = 'ru';
function updateSelect(elem, list) {
var _default = elem.children[0];
while(elem.firstChild) {
elem.removeChild(elem.firstChild);
}
elem.appendChild(_default);
if (list) {
for(var key in list) {
if (list.hasOwnProperty(key)) {
var option = document.createElement('option');
option.innerHTML = option.value = key;
elem.appendChild(option);
}
}
}
return elem;
};
(function(goods, language) {
var _make = document.getElementById('_make');
var _model = document.getElementById('_model');
var _country = document.getElementById('_country');
var optionsData = {};
for(var i = 0; i < goods.length; i++) {
var _vName = goods[i].name[language];
var _vModel = goods[i].model;
var _vCountry = goods[i].country[language];
optionsData[_vName] = optionsData[_vName] || {};
optionsData[_vName][_vModel] = optionsData[_vName][_vModel] || {};
optionsData[_vName][_vModel][_vCountry] = optionsData[_vName][_vModel][_vCountry] || {};
}
updateSelect(_make, optionsData).onchange = function() {
updateSelect(_model, optionsData[_make.value]).onchange();
};
_model.onchange = function() {
updateSelect(_country, (optionsData[_make.value]||{})[_model.value]);
}
})(goods, language);
</script>