var g_Vehicle = {
g_Vehicle1: [
Year: "2011",
Make: "abc",
Model: "Avenger",
SubModel: "def"
],
g_Vehicle2: [
Year: "2012",
Make: "abc",
Model: "200",
SubModel: "deft"
],
g_Vehicle3: [
Year: "2011",
Make: "dfg",
Model: "300",
SubModel: "sde"
],
}
问问题
92 次
4 回答
2
那不是 JSON,那是 JavaScript 对象字面量。无效的,您的语法不正确...
var g_Vehicle = {
g_Vehicle1: [
// v------------------------here
Year: "2011",
Make: "abc",
Model: "Avenger",
SubModel: "def"
],
...
[
...]
是您定义数组的方式,但您放入其中的是您定义对象属性的方式。如果您将[
and更改]
为{
and }
,它将是有效的,但使用起来很尴尬。相反,只需将整个事物设为对象数组:
var g_Vehicles = [
{
Year: "2011",
Make: "abc",
Model: "Avenger",
SubModel: "def"
},
{
Year: "2012",
Make: "abc",
Model: "200",
SubModel: "deft"
},
{
Year: "2011",
Make: "dfg",
Model: "300",
SubModel: "sde"
}
];
然后你可以遍历它,生成你想要的任何类型的下拉菜单。例如,这是创建select
下拉菜单的一种方法:
var markup = ['<select>'];
$.each(g_Vehicles, function(index, vehicle) {
markup.push('<option value=' + index + '>' +
vehicle.Make + '-' + vehicle.Model + '-' + vehicle.SubModel +
'</option>');
});
markup.push('</select>');
$("selector_for_some_container").html(markup.join(""));
于 2012-11-01T08:26:21.570 回答
0
这是一个 JavaScript 对象文字而不是 JSON。
所以使用jQuery的$.each
函数:
g_Vehicle.each(function(index) {
$('select').append($('<option>' + g_Vehicle[index].Make + '</option>'));
});
于 2012-11-01T08:27:22.313 回答
0
这是一些可以帮助您入门的代码。根据您填写的下拉菜单对此进行扩展。
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
var g_Vehicle = [
{
Year: "2011",
Make: "Ram",
Model: "Avenger",
SubModel: "Dart",
Vin: "1C1"
},
{
Year: "2012",
Make: "Dodge",
Model: "200",
SubModel: "Dart",
Vin: "1C2"
},
{
Year: "2011",
Make: "Chrysler",
Model: "300",
SubModel: "Durango",
Vin: "1C3"
}
];
var year="2011";
$(document).ready(function(){
for(var i in g_Vehicle){
if(g_Vehicle[i].Year == year){
$('#make').append($('<option>' + g_Vehicle[i].Make + '</option>'));
}
}
});
</script>
<body>
<select id="make"></select>
</body>
</html>
于 2012-11-01T08:27:30.997 回答
0
与jQuery一起做......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
var g_Vehicle = {
g_Vehicle1: {
Year: "2011",
Make: "abc",
Model: "Avenger",
SubModel: "def"
},
g_Vehicle2: {
Year: "2012",
Make: "abc",
Model: "200",
SubModel: "deft"
},
g_Vehicle3: {
Year: "2011",
Make: "dfg",
Model: "300",
SubModel: "sde"
}
}
$(document).ready(function() {
(function() {
var l = '';
for(var id in g_Vehicle) {
l += '<option value="'+id+'">'+id+'</option>'
}
$('#mymenu')
.html(l)
.change(function() {
//Test here
var r = g_Vehicle[$(this).val()];
alert('Year: ' + r.Year+'\nMake: ' + r.Make+'\nModel: ' + r.Model+'\nSubModel: ' + r.SubModel+'\n');
});
})();
});
</script>
</head>
<body>
<form id="myform" name="myform">
<select id="mymenu" name="mymenu"></select>
</form>
</body>
</html>
于 2012-11-01T08:48:46.687 回答