Problem in JSON Loop

I want to add 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 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 ...

+3
source share
3 answers

It was complicated. If you go undefinedto attr( attr('value', undefined)) or html( html(undefined)), it will be practically the same as calling attr('value')or html(), which return a string, not a jQuery object.

But wait, you say, I do not pass undefined, I pass on the values guruMapelIdand guruNama!

: JSON . , v.guruMapelId (, undefined).

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

guruNama. JSON.

DEMO

+2

JSON- , "guruMapelId ", "guruMapelId". JSON-, var guruMapelId = v["guruMapelId "]; var guruNama = v["guruNama "];.

+4

Check what you get in your $ .getJSON (just use alert (guruMapelId) and guruNama), then you can just make your version as follows:

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

All Articles