如何使用jQuery在选择框中显示JSON数据?

时间:2010-02-21 16:34:55

标签: jquery json

这是我的JSON数据:

{"ACT":"Australian Capital Territory","NSW":"New South Wales","NT":"Northern Territory","QLD":"Queensland","SA":"South Australia","TAS":"Tasmania","VIC":"Victoria"}

如何在选择列表中显示此JSON数据?

2 个答案:

答案 0 :(得分:5)

使用$ .each并迭代所有元素。这应该有效:

$.each(jsonData, function(key, value)
{
   $('<option></option>').val(key).text(value).appendTo('#yourSelectList');
});

答案 1 :(得分:1)

这是一种方式(减少函数调用次数):

var myJson = {"ACT":"Australian Capital Territory","NSW":"New South Wales","NT":"Northern Territory","QLD":"Queensland","SA":"South Australia","TAS":"Tasmania","VIC":"Victoria"};

$.each(myJson,function(key,val) {
    $('#mySelect').append('<option value="'+ key + '">' + val + '</option>');
});