循环 JSON 时出现问题
我想将数据附加到选择框。但是,我收到了这个错误...
“错误:$("").attr("value", guruMapelId).html 不是函数
源文件: http://localhost:8084/controller?aksi=kurikulum
线:5"
这是我的 js 代码:
function dataGuruMapelSelect(dataSelect){
$.getJSON("controller", "aksi=dataGuruMapel", function(json){
$.each(json.guruMapelData, function(k,v){
var guruMapelId = v.guruMapelId;
var guruNama = v.guruNama ;
$('<option />').attr('value',guruMapelId).html(guruNama).appendTo(dataSelect);
})
});
}
这是 JSON 数据
{
"guruMapelData": [
{
"guruMapelId ": "1",
"guruNip ": "1331/001",
"guruNama ": "HARI BUDIYONO DRS.",
"mapelNama ": "PPKn",
"tahunAjarNama ": "2010/2011",
"mapelKategoriNama ": "Normatif",
"mapelId ": "1"
},
{
"guruMapelId ": "2",
"guruNip ": "1331/002",
"guruNama ": "PENI WARDAYANI DRA",
"mapelNama ": "Kewirausahaan",
"tahunAjarNama ": "2010/2011",
"mapelKategoriNama ": "Produktif",
"mapelId ": "2"
}
]
}
我的错是什么? 先谢谢了...
I want to append data to selectbox. But, I got this error...
"Error: $("").attr("value", guruMapelId).html is not a function
Source File: http://localhost:8084/controller?aksi=kurikulum
Line: 5"
this is my js code:
function dataGuruMapelSelect(dataSelect){
$.getJSON("controller", "aksi=dataGuruMapel", function(json){
$.each(json.guruMapelData, function(k,v){
var guruMapelId = v.guruMapelId;
var guruNama = v.guruNama ;
$('<option />').attr('value',guruMapelId).html(guruNama).appendTo(dataSelect);
})
});
}
And this is the JSON data
{
"guruMapelData": [
{
"guruMapelId ": "1",
"guruNip ": "1331/001",
"guruNama ": "HARI BUDIYONO DRS.",
"mapelNama ": "PPKn",
"tahunAjarNama ": "2010/2011",
"mapelKategoriNama ": "Normatif",
"mapelId ": "1"
},
{
"guruMapelId ": "2",
"guruNip ": "1331/002",
"guruNama ": "PENI WARDAYANI DRA",
"mapelNama ": "Kewirausahaan",
"tahunAjarNama ": "2010/2011",
"mapelKategoriNama ": "Produktif",
"mapelId ": "2"
}
]
}
What is my fault?
Thanks before...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 JSON 键的名称后面包含空格,因此该键是
"guruMapelId "
,而不是"guruMapelId"
。您可以删除 JSON 键中的空格,也可以使用var guruMapelId = v["guruMapelId "];
和var guruNama = v["guruNama "];
代替。Your JSON keys contain spaces after their names, so the key is
"guruMapelId "
, not"guruMapelId"
. You can either remove the space from your JSON keys or usevar guruMapelId = v["guruMapelId "];
andvar guruNama = v["guruNama "];
instead.这是一个棘手的问题。如果将
undefined
传递给attr
(attr('value', undefined)
) 或html
(html(undefined)
),它实际上与调用attr('value')
或html()
相同,它们都返回一个字符串而不是一个 jQuery 对象。但是等等,你会说,我没有传递
undefined
,我传递的是guruMapelId
和guruNama 的值
!让我们仔细看看:JSON 中的键都有一个尾随空格。这意味着,
v.guruMapelId
不存在(因此是undefined
)。您必须使用Same for
guruNama
访问数据。但更好的方法是创建正确的 JSON。演示
This was a tricky one. If you pass
undefined
toattr
(attr('value', undefined)
) orhtml
(html(undefined)
), it will actually be the same as callingattr('value')
orhtml()
, which both return a string and not a jQuery object.But wait, you say, I'm not passing
undefined
, I'm passing the values ofguruMapelId
andguruNama
!Let's have a closer look: The keys in the JSON all have a trailing space. That means,
v.guruMapelId
does not exist (hence isundefined
). You would have to access the data withSame for
guruNama
. But better would be to create proper JSON.DEMO
检查您在 $.getJSON (只需使用alert(guruMapelId) 和 guruNama )调用中得到的内容,然后您可以按如下方式进行选择:
Check what you get in your $.getJSON (just use alert(guruMapelId) and guruNama ) call then you can just make your option as follow: