循环 JSON 时出现问题

发布于 2024-11-09 06:59:22 字数 1358 浏览 0 评论 0原文

我想将数据附加到选择框。但是,我收到了这个错误...

“错误:$("").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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨落成白 2024-11-16 06:59:22

您的 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 use var guruMapelId = v["guruMapelId "]; and var guruNama = v["guruNama "]; instead.

骷髅 2024-11-16 06:59:22

这是一个棘手的问题。如果将 undefined 传递给 attr (attr('value', undefined)) 或 html (html(undefined)),它实际上与调用 attr('value')html() 相同,它们都返回一个字符串而不是一个 jQuery 对象。

但是等等,你会说,我没有传递 undefined,我传递的是 guruMapelIdguruNama 的值

让我们仔细看看:JSON 中的键都有一个尾随空格。这意味着,v.guruMapelId 不存在(因此是undefined)。您必须使用

v['guruMapelId '] // <-- note the trailing space

Same for guruNama 访问数据。但更好的方法是创建正确的 JSON。

演示

This was a tricky one. If you pass undefined to attr (attr('value', undefined)) or html (html(undefined)), it will actually be the same as calling attr('value') or html(), which both return a string and not a jQuery object.

But wait, you say, I'm not passing undefined, I'm passing the values of guruMapelId and guruNama!

Let's have a closer look: The keys in the JSON all have a trailing space. That means, v.guruMapelId does not exist (hence is undefined). You would have to access the data with

v['guruMapelId '] // <-- note the trailing space

Same for guruNama. But better would be to create proper JSON.

DEMO

鸢与 2024-11-16 06:59:22

检查您在 $.getJSON (只需使用alert(guruMapelId) 和 guruNama )调用中得到的内容,然后您可以按如下方式进行选择:

$('<option />').val(guruMapelId).text(guruNama)appendTo(dataSelect);

Check what you get in your $.getJSON (just use alert(guruMapelId) and guruNama ) call then you can just make your option as follow:

$('<option />').val(guruMapelId).text(guruNama)appendTo(dataSelect);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文