1

I have a Json response. I need to take each value and append them to the Select tag. Each value separated by comma should be appended as a option in select tag.

Code is :

Jquery:

var dec ={"dc":["One","Two","Three"]};

jQuery.each(dec, function(index, value) {

    $(".request").append("<option value='" + index + "'>" + value + "</option>");  

});

HTML:

<select class="request">

</select>

The above code is appending everything to a single option but not as different options in select tag

4

3 回答 3

1

试试这样

var dec ={"dc":["One","Two","Three"]};
var html = "";
jQuery.each(dec, function(index, value) {

    html += "<option value='" + index + "'>" + value + "</option>";  
});

 $(".request").html( html );
于 2013-04-24T04:55:21.347 回答
0

您需要遍历dc数组。现在你有一个对象,dec有一个子数组dc

jQuery.each(dec.dc, function(index, value) {
    $(".request").append("<option value='" + index + "'>" + value + "</option>");  
});
于 2013-04-24T04:54:04.390 回答
0
var $opt;

For (var i = 0; i < dec.dc.length; i++) {
    $opt = $('<option></option>');
    $opt.text(dec.dc[i]);
    $opt.val(i);
    $('.request').append($opt);
}

尽可能做一个基本的 js 循环,它们更快。

于 2013-04-24T05:04:46.773 回答