Javascript 映射JSON条目以选择选项
我有一个简单的宠物: 和HashMap-字符串值是子项的名称。 在html文件中,我得到了Json格式的hashmap,例如: {乔恩:[{姓名:狗,年龄:5},{姓名:猫,年龄:4},{姓名:鹦鹉,年龄:3}],保罗:[{姓名:鹦鹉,年龄:3}]} 在我的html中有两个选择,第一个代表孩子的名字,第二个代表宠物:Javascript 映射JSON条目以选择选项,javascript,jquery,json,Javascript,Jquery,Json,我有一个简单的宠物: 和HashMap-字符串值是子项的名称。 在html文件中,我得到了Json格式的hashmap,例如: {乔恩:[{姓名:狗,年龄:5},{姓名:猫,年龄:4},{姓名:鹦鹉,年龄:3}],保罗:[{姓名:鹦鹉,年龄:3}]} 在我的html中有两个选择,第一个代表孩子的名字,第二个代表宠物: <select id="Child"> <option value="Jon">Jon</option>
<select id="Child">
<option value="Jon">Jon</option>
<option value="Paul">Paul</option>
</select>
<select id="Pet">
</select>
我还没有测试以下内容,但它可能会像编写的那样工作。有关更多说明,请参见内联注释:
// Only proceed once the DOM has been constructed
document.addEventListener('DOMContentLoaded', function () {
// Parse the JSON string into a proper object
let data = JSON.parse('{"Jon":[...],"Paul":[...]}');
// References to both SELECT elements
let child = document.getElementById('Child');
let pet = document.getElementById('Pet');
// An event listener to handle changes to the child-select
if (child && pet) {
child.addEventListener('change', function () {
// Attempt to look-up the selected value in our JSON
let option = data[this.value];
// Proceed only if a match (with depth) is found
if (option && option.length) {
// Create a fragment, and add option elements
let fragment = document.createDocumentFragment();
option.forEach(function (item) {
let entry = document.createElement("option");
entry.value = item.name;
entry.textContent = item.name;
fragment.appendChild(entry);
});
// Clear the pet-select of any values, re-populate
pet.innerHTML = '';
pet.appendChild(fragment);
}
});
}
});
如果您试图将宠物链接到它们的主人,您可以使用JQuery链接插件 var值={ 乔恩:[{姓名:狗,年龄:5},{姓名:猫,年龄:4},{姓名:鹦鹉,年龄:3}], 保罗:[{名字:鹦鹉,年龄:3}] } $function{ 对于var输入值{ 对于变量i=0;i
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
// Parse the JSON string into a proper object
let data = JSON.parse('{"Jon":[{"name":"dog","age":5},{"name":"cat","age":4},{"name":"parrot","age":3}],"Paul":[{"name":"parrot","age":3}]}');
// References to both SELECT elements
let child = document.getElementById('Child');
let pet = document.getElementById('Pet');
// An event listener to handle changes to the child-select
child.addEventListener('change', function () {
// Attempt to look-up the selected value in our JSON
let option = data[this.value];
// Proceed only if a match (with depth) is found
if (option && option.length) {
// Create a fragment, and add option elements
let fragment = document.createDocumentFragment();
option.forEach(function (item) {
let entry = document.createElement("option");
entry.value = item.name;
entry.textContent = item.name;
fragment.appendChild(entry);
});
// Clear the pet-select of any values, re-populate
pet.innerHTML = '';
pet.appendChild(fragment);
}
});
</script>
</head>
<body>
<select id="Child">
<option value="Jon">Jon</option>
<option value="Paul">Paul</option>
</select id="Pet">
<select id="Pet">
</select>
</body>
</html>
// Only proceed once the DOM has been constructed
document.addEventListener('DOMContentLoaded', function () {
// Parse the JSON string into a proper object
let data = JSON.parse('{"Jon":[...],"Paul":[...]}');
// References to both SELECT elements
let child = document.getElementById('Child');
let pet = document.getElementById('Pet');
// An event listener to handle changes to the child-select
if (child && pet) {
child.addEventListener('change', function () {
// Attempt to look-up the selected value in our JSON
let option = data[this.value];
// Proceed only if a match (with depth) is found
if (option && option.length) {
// Create a fragment, and add option elements
let fragment = document.createDocumentFragment();
option.forEach(function (item) {
let entry = document.createElement("option");
entry.value = item.name;
entry.textContent = item.name;
fragment.appendChild(entry);
});
// Clear the pet-select of any values, re-populate
pet.innerHTML = '';
pet.appendChild(fragment);
}
});
}
});