Javascript 使用json对象附加select选项值和文本

Javascript 使用json对象附加select选项值和文本,javascript,jquery,json,Javascript,Jquery,Json,我已经从服务器接收到json对象,如下所示 {"locations": [{"locationCode": "Branch 1","locationName": "BR-001"}, {"locationCode": "Branch 2","locationName": "BR-002"}, {"locationCode": "Branch 3","locationName": "BR-003"} ]}

我已经从服务器接收到json对象,如下所示

{"locations": [{"locationCode": "Branch 1","locationName": "BR-001"},

               {"locationCode": "Branch 2","locationName": "BR-002"},

               {"locationCode": "Branch 3","locationName": "BR-003"}
              ]}
然后我需要为值添加locationCode,为文本添加locationName

<option value="locationCode">locationName</option>
locationName
我试过这个,但做不到

    var location = data.locations;
        $.each(location, function (key, value) {
        $('#newLocation')
          .append($("<option></option>")
          .attr("value", key)
          .text(value));
        });
var位置=data.locations;
$。每个(位置、功能(键、值){
$(“#新位置”)
.append($(“”)
.attr(“值”,键)
.文本(值));
});
试试这个:

var locations = 
  [
    {"locationCode": "Branch 1","locationName": "BR-001"},

    {"locationCode": "Branch 2","locationName": "BR-002"},

    {"locationCode": "Branch 3","locationName": "BR-003"}
  ];


for (var i=0;i<locations.length;i++){
    var value = locations[i].locationCode;
    var text  = locations[i].locationName;
    var opt = "<option value='"+value+"' >"+text+"</option>";
    $('#newLocation').append(opt);
}
var位置=
[
{“locationCode”:“分支机构1”,“locationName”:“BR-001”},
{“locationCode”:“Branch 2”,“locationName”:“BR-002”},
{“locationCode”:“分支3”,“locationName”:“BR-003”}
];
对于(var i=0;i尝试以下方法:

var locations = 
  [
    {"locationCode": "Branch 1","locationName": "BR-001"},

    {"locationCode": "Branch 2","locationName": "BR-002"},

    {"locationCode": "Branch 3","locationName": "BR-003"}
  ];


for (var i=0;i<locations.length;i++){
    var value = locations[i].locationCode;
    var text  = locations[i].locationName;
    var opt = "<option value='"+value+"' >"+text+"</option>";
    $('#newLocation').append(opt);
}
var位置=
[
{“locationCode”:“分支机构1”,“locationName”:“BR-001”},
{“locationCode”:“Branch 2”,“locationName”:“BR-002”},
{“locationCode”:“分支3”,“locationName”:“BR-003”}
];

对于(var i=0;i您正在迭代数组,即
位置
不在地图上 所以把你的代码改成

var location = data.locations;
        $.each(location, function (index) {
        $('#newLocation')
          .append($("<option></option>")
          .attr("value", location[index].locationCode)
          .text(location[index].locationName));
        });
var位置=data.locations;
$。每个(位置、功能(索引){
$(“#新位置”)
.append($(“”)
.attr(“值”,位置[索引].locationCode)
.text(位置[index].locationName));
});
更新:-


您正在迭代数组,即
位置
不在地图上 所以把你的代码改成

var location = data.locations;
        $.each(location, function (index) {
        $('#newLocation')
          .append($("<option></option>")
          .attr("value", location[index].locationCode)
          .text(location[index].locationName));
        });
var位置=data.locations;
$。每个(位置、功能(索引){
$(“#新位置”)
.append($(“”)
.attr(“值”,位置[索引].locationCode)
.text(位置[index].locationName));
});
更新:-

尝试此解决方案在Stackoverflow上价值较低,因为它们对未来数千名研究人员的教育/授权作用甚微。请改进您的答案,包括您的解决方案如何工作以及为什么这是一个好主意。尝试此解决方案在Stackoverflow上价值较低,因为它们对未来数千名研究人员的教育/授权作用甚微。请改进您的答案,包括您的解决方案如何工作以及为什么它是一个好主意。