gpt4 book ai didi

javascript - 根据json填充select元素

转载 作者:行者123 更新时间:2023-11-30 06:38:05 26 4
gpt4 key购买 nike

如何填充第二个选择元素?我已经想出了如何做第一个。但是我怎么能根据选择的“Make”对第二个做同样的事情呢?我试着一步一步地说服自己完成它,但我认为这对我来说可能太高级了。

var cars = '{"USED":[{"name":"Acura","value":"20001","models":[{"name":"CL","value":"20773"},{"name":"ILX","value":"47843"},{"name":"ILX Hybrid","value":"48964"},{"name":"Integra","value":"21266"},{"name":"Legend","value":"21380"},{"name":"MDX","value":"21422"},{"name":"NSX","value":"21685"},{"name":"RDX","value":"21831"},{"name":"RL","value":"21782"},{"name":"RSX","value":"21784"},{"name":"SLX","value":"21879"},{"name":"TL","value":"22237"},{"name":"TSX","value":"22248"},{"name":"Vigor","value":"22362"},{"name":"ZDX","value":"32888"}]},{"name":"Alfa Romeo","value":"20047","models":[{"name":"164","value":"20325"},{"name":"8c Competizione","value":"34963"},{"name":"Spider","value":"22172"}]}';
var carobj = eval ("(" + cars + ")");
var select = document.getElementsByTagName('select')[0];

//print array elements out
for (var i = 0; i < carobj.USED.length; i++) {
var d = carobj.USED[i];
select.options.add(new Option(d.name, i))
};

最佳答案

如果我没看错你的问题,你想用第一个选择中的品牌模型填充第二个选择。请参阅下面的纯 JS 方法(使用 jsfiddle)。如果可能的话,我建议研究一下 jQuery,因为我更喜欢 jQuery 解决方案。

http://jsfiddle.net/m5U8r/1/

var carobj;

window.onload = function () {
var cars = '{"USED":[{"name":"Acura","value":"20001","models":[{"name":"CL","value":"20773"},{"name":"ILX","value":"47843"},{"name":"ILX Hybrid","value":"48964"},{"name":"Integra","value":"21266"},{"name":"Legend","value":"21380"},{"name":"MDX","value":"21422"},{"name":"NSX","value":"21685"},{"name":"RDX","value":"21831"},{"name":"RL","value":"21782"},{"name":"RSX","value":"21784"},{"name":"SLX","value":"21879"},{"name":"TL","value":"22237"},{"name":"TSX","value":"22248"},{"name":"Vigor","value":"22362"},{"name":"ZDX","value":"32888"}]},{"name":"Alfa Romeo","value":"20047","models":[{"name":"164","value":"20325"},{"name":"8c Competizione","value":"34963"}, {"name":"Spider","value":"22172"}]}]}';

carobj = eval ("(" + cars + ")");
var makes = document.getElementById('make');

for (var i = 0; i < carobj.USED.length; i++) {
var d = carobj.USED[i];
makes.options.add(new Option(d.name, i));
}

makes.onchange = getModels;
getModels();
}

// add models based on make
function getModels () {
var makes = document.getElementById('make');
var make = makes.options[makes.selectedIndex].text;
for (var i = 0; i < carobj.USED.length; i++) {
if (carobj.USED[i].name == make) {
var models = document.getElementById('model');
models.options.length = 0;
for (var j= 0; j < carobj.USED[i].models.length; j++) {
var model = carobj.USED[i].models[j];
models.options.add(new Option(model.name, j));
}
break;
}
}
}

我还建议研究更安全的 JSON 解析。如果 eval 在任何用户输入上运行,则使用 eval 存在安全风险。你可以看看 JSON.org和他们的 json2.js .或者,如果您想使用 jQuery:parseJSON .以下是 jQuery 版本:

jQuery.parseJSON(jsonString);

JSON 解析技巧来自:Safely turning a JSON string into an object .

关于javascript - 根据json填充select元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13327933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com