所以我有JSON数组访问JSON数组元素
data = '[{"beatles": [
{"name":"Paul McCartney","value": "http://www.paulmccartney.com"},
{"name": "John Lennon","value": "http://www.johnlennon.it"},
{"name":"George Harrison","value": "http://www.georgeharrison.com"},
{"name": "Ringo Starr","value": "http://www.ringostarr.com"}],
"stones": [
{"name": "Mick Jagger","value": "http://www.mickjagger.com"},
{"name": "Keith Richards","value": "http://www.keithrichards.com"}]'
和我有两个下拉列表
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="rockbands.json"></script>
<script src="script.js"></script>
</head>
<body onload="getBands()">
<select id="bands" onchange="getNames(this.value)"></select>
<select id="names" ></select>
</body>
</html>
所以我加载具有ID“带”的下拉列表与此JS代码
function getBands() {
var root = JSON.parse(data)
var bandsBox = document.getElementById("bands")
for(i in root[0]) {
var option = document.createElement("option")
option.text = i
bandsBox.appendChild(option)
}
}
我想要做的是填充ID为“名称”的其他下拉列表 与数组elem基于在第一个下拉列表中的选择,例如,如果我选择了披头士它将加载所有名称披头士012“一个动态的方式来访问他们的感谢
那不是一个JSON数组,它是一个字符串。 – Craicerjack
啊对不起,json字符串多数民众赞成我的意思是 – Willy
看到这个 - https://jsfiddle.net/abhitalks/05ejeunh/ – Abhitalks