0

ajax経由でページに来るデータの文字列があります。次の形式で

[{"servername":"svrA"},{"servername":"svrB"},{"servername":"svrC"},{"location":"locA"},{"location":"locB"},{"location":"locC"}]

2 つの選択ボックスにデータを入力したいと思います。

次の疑似コードのさまざまなバリエーションを試しました。

for each item in the json string
if obj == servername
   add to selectbox Server
if obj == location
   add to selectbox Location

どんなアイデアでも大歓迎です。ありがとう。

4

2 に答える 2

1
var stuff = [{"servername":"svrA"},{"servername":"svrB"},{"servername":"svrC"},{"location":"locA"},{"location":"locB"},{"location":"locC"}];

var elems = {
    "servername": jQuery('#select-server'),
    "location": jQuery('#select-location')
};

stuff.forEach(function(item){
    for(var selectName in elems){
        if(typeof item[selectName] != 'undefined'){
            var val = item[selectName];
            elems[selectName].append(jQuery('<option/>').val(val).html(val));
        }
    }
});

forEach古いブラウザでは利用できないわけではありません。上記のコードは、作業用の単なるサンプルです。

于 2013-08-25T21:05:04.120 に答える