将数组解析为SELECT OPTIONS

时间:2016-02-08 15:58:57

标签: jquery arrays

我有一个这样的数组:

["News & Politics", "Personal Journals", "Society & Culture", "Technology", "Arts", "Natural Sciences", "Business", "Comedy", "History", "Tech News", "TV & Film", "Design", "Performing Arts", "Education", "Christianity", "Investing", "Sports & Recreation", "Professional", "Social Sciences", "Automotive", "Science & Medicine", "Management & Marketing", "Sexuality", "Philosophy", "Self-Help", "Literature", "Buddhism", "Places & Travel", "Music", "Language Courses", "Careers", "Food", "Kids & Family", "National"]

我需要"推"将这些值转换为select,数组中的每个项目都是option

我该如何完成这项工作?

数组存储为变量all_cat_unique,但(当然)以下内容不起作用。我只是将整个数组附加到选择中。

$.each(obj.entry, function(i, data) {

    $('#all-categories').append('<option>'+all_cat_unique+'</option>');
});

感谢。

4 个答案:

答案 0 :(得分:2)

给定数组:

var all_cat_unique = ["News & Politics", "Personal Journals", "Society & Culture", "Technology", "Arts", "Natural Sciences", "Business", "Comedy", "History", "Tech News", "TV & Film", "Design", "Performing Arts", "Education", "Christianity", "Investing", "Sports & Recreation", "Professional", "Social Sciences", "Automotive", "Science & Medicine", "Management & Marketing", "Sexuality", "Philosophy", "Self-Help", "Literature", "Buddhism", "Places & Travel", "Music", "Language Courses", "Careers", "Food", "Kids & Family", "National"];

HTML选择:

<select id="all-categories"></select>

您可以使用以下代码将数组中的选项动态附加到选择:

for(var i = 0; i < all_cat_unique.length; i++)
$('#all-categories').append( $("<option></option>").attr("value",all_cat_unique[i]).text(all_cat_unique[i]) );

以下是您的给定数组jsfiddle。希望它有所帮助

答案 1 :(得分:1)

var all_cat_unique = ["News & Politics", "Personal Journals", "Society & Culture", "Technology", "Arts", "Natural Sciences", "Business", "Comedy", "History", "Tech News", "TV & Film", "Design", "Performing Arts", "Education", "Christianity", "Investing", "Sports & Recreation", "Professional", "Social Sciences", "Automotive", "Science & Medicine", "Management & Marketing", "Sexuality", "Philosophy", "Self-Help", "Literature", "Buddhism", "Places & Travel", "Music", "Language Courses", "Careers", "Food", "Kids & Family", "National"];

$.each(all_cat_unique, function(i, data) {
    $('#all-categories').append('<option value="' + i + '">' + data + '</option>');
});

/*get value selected*/
$("#all-categories").on("change", function() {
    alert("Selected: " + all_cat_unique[this.value]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="all-categories"></select>

答案 2 :(得分:-1)

好老的循环

var arr = ["News & Politics", "Personal Journals", "Society & Culture",     "Technology", "Arts", "Natural Sciences", "Business", "Comedy", "History", "Tech News", "TV & Film", "Design", "Performing Arts", "Education", "Christianity", "Investing", "Sports & Recreation", "Professional", "Social Sciences", "Automotive", "Science & Medicine", "Management & Marketing", "Sexuality", "Philosophy", "Self-Help", "Literature", "Buddhism", "Places & Travel", "Music", "Language Courses", "Careers", "Food", "Kids & Family", "National"];


for(var i =  0; i< arr.length;i++){

$('#all-categories').append('<option>'+arr[i]+'</option>');

}

答案 3 :(得分:-2)

你走了。

$.each(obj.entry, function(i, data) {

    $('#all-categories').append('<option>'+data+'</option>');
});

i =是索引。

data = value。

https://jsfiddle.net/xqa7w557/