1 回答
TA贡献1824条经验 获得超5个赞
该脚本会将您的数据转换为下拉菜单中的两步选择
var data = {
"Afghanistan": {
"Persian": "https://www.mysite.af/lang=fa",
"Pushto": "https://www.mysite.af/lang=ps",
"Pashto": "https://www.mysite.af/lang=ps"
},
"Albania": {
"Albanian": "https://www.mysite.al/lang=sq",
"English": "https://www.mysite.al/lang=en"
},
"United Kingdom of Great Britain and Northern Ireland": {
"English": "https://www.mysite.co.uk/lang=en"
},
"United Arab Emirates": {
"Arabic": "https://www.mysite.ae/lang=ar",
"English": "https://www.mysite.ae/lang=en",
"Hindi": "https://www.mysite.ae/lang=hi",
"Persian": "https://www.mysite.ae/lang=fa",
"Urdu": "https://www.mysite.ae/lang=ur"
}
}
var firstChoice = document.getElementById('first_choice');
var first = Object.keys(data);
for (var i = 0; i < first.length; i++) {
firstChoice.innerHTML += '<option value="' + first[i] + '">' + first[i] + '</option>';
}
firstChoice.addEventListener("change", function () {
if (this.value.length > 0) {
secondDropDown(this.value);
} else {
var sec = document.getElementById('second_choice');
sec.innerHTML = '';
}
});
function secondDropDown(x) {
var sec = document.getElementById('second_choice');
sec.innerHTML = '<option selected>Please Select Language</option>';
var y = data[x];
for (const [key, value] of Object.entries(y)) {
sec.innerHTML += '<option value="' + value + '">' + key + '</option>';
}
}
<select id="first_choice">
<option selected value="">Please Select Country</option>
<!-- Appended options -->
</select>
<select id="second_choice">
</select>
添加回答
举报