2013-05-08 168 views
41

我在我的java sevlet中有一張地圖並將其轉換爲正確的json格式。jQuery--從json填充選擇

當我在下面執行此功能時,會創建一個下拉菜單,但它會將每個字符作爲選項? 這是我的了:JSON格式

$(document).ready(function(){ 
    var temp= '${temp}'; 
    //alert(options); 
    var $select = $('#down');       
    $select.find('option').remove();       
    $.each(temp, function(key, value) {    
     $('<option>').val(key).text(value).appendTo($select);  
    }); 
}); 

地圖內容

{"1" : "string","2" : "string"} 
+0

確定臨時變量是正確的您可以在插件添加的功能呢? – FishBasketGordo 2013-05-08 13:52:13

+1

你不是讀一個json對象,而是一個字符串,所以$ .each得到了一個字符串的每個字母。嘗試console.log(temp);所以你可以有一個你想要的對象的想法 – joao 2013-05-08 13:52:16

+4

你可能還需要爲你的臨時變量做一個'JSON.parse'來將它從一個字符串轉換爲一個實際的js對象。 – 2013-05-08 14:00:47

回答

59

我會做這樣的事情:

$.each(temp,function(key, value) 
{ 
    $select.append('<option value=' + key + '>' + value + '</option>'); 
}); 

JSON結構,將不勝感激。起初你可以試用find('element') - 它取決於JSON。

+4

它應該是$ .each(temp,function(key,value) – NullPointerException 2013-05-08 13:58:48

+0

'temp'不是一個jQuery對象...它沒有.each或.find。 – 2013-05-08 13:59:27

+0

是的,代碼編輯。 – 2013-05-08 14:00:32

2

fiddle

var $select = $('#down'); 
$select.find('option').remove(); 
$.each(temp,function(key, value) 
{ 
    $select.append('<option value=' + key + '>' + value + '</option>'); 
}); 
+0

這會工作與他已有的一樣,他的'temp'變量有問題,而不是他如何生成選項。 – 2013-05-08 13:58:49

+0

究竟.....我得到了同樣的東西 – 2013-05-08 14:00:47

+1

如果他發佈的JSON映射是正確的,那麼代碼應該運行正常 – NullPointerException 2013-05-08 14:02:41

1

我只是用JavaScript控制檯在Chrome中做到這一點。我用佔位符替換了一些你的東西。

var temp= ['one', 'two', 'three']; //'${temp}'; 
//alert(options); 
var $select = $('<select>'); //$('#down');       
$select.find('option').remove();       
$.each(temp, function(key, value) {    
    $('<option>').val(key).text(value).appendTo($select); 
}); 
console.log($select.html()); 

輸出:

<選項值= 「0」>一個< /選項> <選項值= 「1」> 2 < /選項> <選項值= 「2」> 3 < /選項>

但是它看起來像你的JSON可能是一個字符串,因爲以下最終會做你的描述。所以讓你的JSON實際的JSON不是一個字符串。

var temp= "['one', 'two', 'three']"; //'${temp}'; 
//alert(options); 
var $select = $('<select>'); //$('#down');       
$select.find('option').remove();       
$.each(temp, function(key, value) {    
    $('<option>').val(key).text(value).appendTo($select); 
}); 
console.log($select.html()); 
0

我相信這可以幫助你:

$(document).ready(function(){ 
    var temp = {someKey:"temp value", otherKey:"other value", fooKey:"some value"}; 
    for (var key in temp) { 
     alert('<option value=' + key + '>' + temp[key] + '</option>'); 
    } 
}); 
23

只有改變DOM一次...

var listitems = ''; 
$.each(temp, function(key, value){ 
    listitems += '<option value=' + key + '>' + value + '</option>'; 
}); 
$select.append(listitems); 
+0

性能最好 – 2015-04-29 08:50:55

+8

您必須執行var listitems ='';'''或者您得到一個undefined concat – 2015-04-29 09:04:54

+0

這應該是公認的答案 – SiamKreative 2016-12-27 09:00:35

0

,在阿賈克斯做工精細回調從JSON對象

更新選擇
function UpdateList() { 
    var lsUrl = '@Url.Action("Action", "Controller")'; 

    $.get(lsUrl, function (opdata) { 

     $.each(opdata, function (key, value) { 
      $('#myselect').append('<option value=' + key + '>' + value + '</option>'); 
     }); 
    }); 
} 
4

一種解決方案是CREA你自己的jquery插件採用json地圖並用它填充選擇。

(function($) {  
    $.fn.fillValues = function(options) { 
     var settings = $.extend({ 
      datas : null, 
      complete : null, 
     }, options); 

     this.each(function(){ 
      var datas = settings.datas; 
      if(datas !=null) { 
       $(this).empty(); 
       for(var key in datas){ 
        $(this).append('<option value="'+key+'"+>'+datas[key]+'</option>'); 
       } 
      } 
      if($.isFunction(settings.complete)){ 
       settings.complete.call(this); 
      } 
     }); 

    } 

}(jQuery)); 

您可以通過這樣稱呼它:

$("#select").fillValues({datas:your_map,}); 

的優點是,任何地方,你也會面臨同樣的問題,你只需要調用

$("....").fillValues({datas:your_map,}); 

的Et瞧!

,只要你喜歡

0
$(JSON.parse(response)).map(function() { 
    return $('<option>').val(this.value).text(this.label); 
}).appendTo('#selectorId'); 
+0

僅供參考,現在不推薦使用$ .parseJSON。本機JSON.parse()是首選。 – David 2017-09-19 15:07:38