开发者

How to display JSON data in a select box using jQuery?

开发者 https://www.devze.com 2022-12-20 16:39 出处:网络
This is my JSON data: { \"ACT\": \"Australian Capital Territory\", \"NSW\": \"New South Wales\", \"NT\": \"Northern Territory\",

This is my JSON data:

{
  "ACT": "Australian Capital Territory",
  "NSW": "New South Wales",
  "NT": "Northern Territory",
  "QLD": "Queensland",
  "开发者_StackOverflow社区SA": "South Australia",
  "TAS": "Tasmania",
  "VIC": "Victoria"
}

How to display this JSON data in a selectlist?


Use $.each and iterate over all your elements. This should work:

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


Here's one way (takes fewer function calls):

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>');
});
0

精彩评论

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