我正在尝试通过遍历JSON对象并使用value=
附加每个<option>
的row.id
和使用<option>
在row.name
标记之间附加实际值来填充下拉列表。
编辑
我忘了提到传递的数据已经过Ajaxed,这是PHP调用数据库的结果。
问题在于,在填充列表时,每个条目都被列为undefined
。也是id
的同上。我一定做错了,我不确定是什么。这是我有的数组:
[{
"id": "1",
"0": "1",
"parent": "0",
"1": "0",
"name": "Automotive",
"2": "Automotive",
"description": null,
"3": null,
"ordering": "0",
"4": "0",
"pub
lished": "1",
"5": "1"
}, {
"id": "2",
"0": "2",
"parent": "0",
"1": "0",
"name": "Aerospace",
"2": "Aerospace",
"description": null,
"3": null,
"ordering
": "1",
"4": "1",
"published": "1",
"5": "1"
}, {
"id": "3",
"0": "3",
"parent": "0",
"1": "0",
"name": "Agricultural",
"2": "Agricultural",
"description": null,
"3": null,
"ordering": "2",
"4": "2",
"published": "1",
"5": "1"
}, {
"id": "4",
"0": "4",
"parent": "0",
"1": "0",
"name": "Audiovisual",
"2": "Audiovisual",
"description": null,
"3": null,
"ordering": "3",
"4": "3",
"published": "1",
"5": "1"
}, {
"id": "5",
"0": "5",
"parent": "0",
"1": "0",
"name": "
Construction",
"2": "Construction",
"description": null,
"3": null,
"ordering": "4",
"4": "4",
"published": "1",
"5": "1"
}, {
"id": "6",
"0": "6",
"parent": "0",
"1": "0",
"name": "Energy",
"2": "Energy",
"description": null,
"3": null,
"ordering": "6",
"4": "6",
"published": "1",
"5": "1"
}, {
"id": "7",
"
0": "7",
"parent": "0",
"1": "0",
"name": "Electronics",
"2": "Electronics",
"description": null,
"3": null,
"ordering": "7",
"4": "7",
"published": "1
",
"5": "1"
}, {
"id": "8",
"0": "8",
"parent": "0",
"1": "0",
"name": "Electrical",
"2": "Electrical",
"description": null,
"3": null,
"ordering": "8",
"4
": "8",
"published": "1",
"5": "1"
}, {
"id": "9",
"0": "9",
"parent": "0",
"1": "0",
"name": "Entertainment",
"2": "Entertainment",
"description": null,
"3": null,
"ordering": "9",
"4": "9",
"published": "1",
"5": "1"
}, {
"id": "10",
"0": "10",
"parent": "0",
"1": "0",
"name": "Manufacturing",
"2": "Manufa
cturing",
"description": null,
"3": null,
"ordering": "9",
"4": "9",
"published": "1",
"5": "1"
}, {
"id": "11",
"0": "11",
"parent": "0",
"1": "0",
"name": "Medical",
"2": "Medical",
"description": null,
"3": null,
"ordering": "11",
"4": "11",
"published": "1",
"5": "1"
}, {
"id": "12",
"0": "12",
"parent": "0",
"1": "0",
"name": "Marine",
"2": "Marine",
"description": null,
"3": null,
"ordering": "12",
"4": "12",
"published": "1",
"5": "1"
}, {
"id": "13",
"0": "13",
"parent": "0",
"1": "0",
"name": "Home Applicances",
"2": "Home Applicances",
"description": null,
"3": null,
"ordering": "13",
"4": "13",
"published": "1",
"5": "1"
}, {
"id": "14",
"0": "14",
"parent": "0",
"1": "0",
"name": "Software",
"2": "Software",
"description": null,
"3": null,
"ordering": "14",
"4": "14",
"published": "1",
"5": "1"
}, {
"id": "15",
"0": "15",
"parent": "0",
"1": "0",
"name": "Theoretical work",
"2": "Theoretical work",
"description": null,
"3": null,
"ordering": "15",
"4": "15",
"published": "1",
"5": "1"
}, {
"id": "16",
"0": "16",
"parent": "0",
"1": "0",
"name": "Railroad",
"2": "Railroad",
"description": null,
"3": null,
"ordering": "16",
"4": "16",
"published": "1",
"5": "1"
}, {
"id": "77",
"0": "77",
"parent": "0",
"1": "0",
"name": "Chemistry",
"2": "Chemistry",
"description": null,
"3": null,
"ordering": "5",
"4": "5",
"published": "1",
"5": "1"
}, {
"id": "158",
"0": "158",
"parent": "0",
"1": "0",
"name": "Empty Category",
"2": "Empty Category",
"description": null,
"3": null,
"ordering": "17",
"4": "17",
"published": "1",
"5": "1"
}]
这是我的代码:
function printSubCategories(categories) {
for (var row in categories) {
rowParsed = jQuery.parseJSON(row);
var opt = document.createElement('option');
if (categories.hasOwnProperty(row)) {
opt.text = rowParsed.name;
opt.value = rowParsed.id;
}
jQuery('#subcategories').append(opt);
}
}
我到底在做什么错?
更新
尝试了以下实现:
if (categories.hasOwnProperty(row)) {
idParsed = jQuery.parseJSON(row.id);
nameParsed = jQuery.parseJSON(row.name)
opt.text = idParsed;
opt.value = nameParsed;
}
还是行不通...
最佳答案
数据的意外区域中有一些换行符。例如,例如
[{
"id": "1",
"0": "1",
"parent": "0",
"1": "0",
"name": "Automotive",
"2": "Automotive",
"description": null,
"3": null,
"ordering": "0",
"4": "0",
"pub
lished": "1",
"5": "1"
}
published
中有一个换行符,这使JSON无效。删除所有这些换行符应该使JSON有效,并且可以再次对其进行解析。在这里测试您的json,看看它是否是有效的json
http://jsonlint.com/
编辑:
好的,我已经尝试过jsfiddle上的代码,然后使我意识到json的实际格式。您在数组中有一些对象,这意味着您只需要循环数组,然后按通常的方式访问该对象。
看到这个jsfiddle
http://jsfiddle.net/vErj4/