开发者

jquery + ajax + json + fill dropdown list not working

开发者 https://www.devze.com 2022-12-11 10:20 出处:网络
I\'m pretty sure i\'am almost there....but i cannot figure out how to iterate through json objects and fill a dropdown list. Here is the js cod开发者_如何学Pythone:

I'm pretty sure i'am almost there....but i cannot figure out how to iterate through json objects and fill a dropdown list. Here is the js cod开发者_如何学Pythone:

My JSON data returned:

{"name":"County1","name":"County1","name":"County1"}

$(document).ready(function() { $("#ddlCountries").change(function() { $("#ddl2").html(""); $.ajax({ type: "GET", url: "Handler.ashx?", data: "county=" + $("#ddlCountries option:selected").text(), contentType: "application/json; charset=utf-8", dataType: "json", success: function(countyList) { $.each(countyList, function() { $("#ddl2").append(' + this['name'] + ''); }); }, error: function(XMLHttpRequest, textStatus) { alert(textStatus); } }); }); });

i'm sure is something simple but as i am a newbie on this i'm not beeing able to get it work.

Appreciate your help guys!

Br, Teixeira


Your JSON data is invalid. You can't have multiple instances of the same property in an object.

You probably want:

[ 
    {
        "name": "Country1"
    },
    {
        "name": "Country1"
    },
    {
        "name": "Country1"
    }
]

Or even:

[ "Country1", "Country1", "Country1" ]

You can loop over it as per the example for for in the spec.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号