[英]Dynamically build html select from json + select option
I have a jquery ajax call that will be pulling back json data such as this: 我有一个jquery ajax调用,它将回拉json数据,例如:
{
"Title":"The Office",
"Season":"1",
"totalSeasons":"9",
"Episodes":[
{
"Title":"Pilot",
"Released":"2005-03-24",
"Episode":"1",
"imdbRating":"7.6",
"imdbID":"tt0664521"
},
{
"Title":"Diversity Day",
"Released":"2005-03-29",
"Episode":"2",
"imdbRating":"8.3",
"imdbID":"tt0664514"
},
{
"Title":"Health Care",
"Released":"2005-04-05",
"Episode":"3",
"imdbRating":"7.9",
"imdbID":"tt0664517"
},
],
"Response":"True"
}
From that data I wish to build an html select bound to it. 从该数据,我希望建立绑定到它的html选择。 For example:
例如:
<select>
<option value="tt0664521">Pilot - Episode 1</option>
<option value="tt0664514">Diversity Day - Episode 2</option>
<option value="tt0664517">Health Care - Episode 3</option>
</select>
My js code will often (though not always) know the episode number already, which maps to the "Episode" data element in the json. 我的js代码将经常(尽管并非总是)知道情节编号,该情节编号映射到json中的“ Episode”数据元素。 When I know the episode, I want to pre-select that option within the select.
当我知道情节时,我想在选择中预先选择该选项。 If I don't know it, no pre-select.
如果我不知道,则无需预先选择。 In all cases I want to create an option for each record.
在所有情况下,我都想为每个记录创建一个选项。 This has to happen dynamically in the js because I won't be able to get back this json until the user enters some data.
这必须在js中动态发生,因为在用户输入一些数据之前,我将无法取回此json。
I have the ajax calls all working, I just need to know how to dynamically add options to my select bound to that data, and then to auto-select an option when I know the episode. 我已经使ajax调用全部正常工作,我只需要知道如何向绑定到该数据的选择中动态添加选项,然后在知道该情节时自动选择一个选项。
A pure jquery solution would be ideal, but I'm happy with just js also. 一个纯jquery解决方案将是理想的选择,但我也对js感到满意。
How is it done? 怎么做? (And thank you!)
(谢谢!)
Note: This is not a duplicate of this post . 注意:这不是此文章的重复。 In my question here, binding to json data is a key piece of the puzzle.
在这里我的问题是,绑定到json数据是难题的关键。
You have to use $.each
method in combination with append
. 您必须结合使用
$.each
方法和append
。
$.each
method represents a generic iterator function which accepts a callback function. $.each
方法表示一个通用的迭代器函数,该函数接受一个回调函数。
append
is used to insert content, specified by the parameter, to the end of each element in the set of matched elements. append
用于将参数指定的内容插入到匹配元素集中每个元素的末尾。
Also, I used map method in order to create select options array. 另外,我使用map方法来创建选择选项数组。
let selectOptions=obj.Episodes.map(function(episode){
return {"value": episode.imdbID,"name":episode.Title+' - '+' Episode '+episode.Episode};
});
Here is the entire solution: 这是整个解决方案:
let obj={ "Title":"The Office", "Season":"1", "totalSeasons":"9", "Episodes":[ { "Title":"Pilot", "Released":"2005-03-24", "Episode":"1", "imdbRating":"7.6", "imdbID":"tt0664521" }, { "Title":"Diversity Day", "Released":"2005-03-29", "Episode":"2", "imdbRating":"8.3", "imdbID":"tt0664514" }, { "Title":"Health Care", "Released":"2005-04-05", "Episode":"3", "imdbRating":"7.9", "imdbID":"tt0664517" }, ], "Response":"True" } let selectOptions=obj.Episodes.map(function(episode){ return {"value": episode.imdbID,"name":episode.Title+' - '+' Episode '+episode.Episode}; }); let select=$('<select>'); $.each(selectOptions,function(index, item){ select.append('<option value="'+item.value+'">'+item.name+'</option>'); }); $('body').append(select);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
For creating the list specifically: 为了专门创建列表:
//Create an empty array.
var list = [];
//Add each option from JSON to the list array
$.each(jsonData, function (key, value) {
list.push(key);
});
//For every option in the list, add it into the select menu
$.each(list, function (key, value) {
$("#my-select-element").append("<option value='" + value + "'>" + value + "</option>");
});
Javascript ES5 solution. Javascript ES5解决方案。 Do not forget to give an id to your select ("my_select"):
不要忘了给您的选择(“ my_select”)提供一个ID:
var data = {
"Title": "The Office",
"Episodes": [
{
"Title": "Pilot",
"Episode": "1",
"imdbID": "tt0664521",
//...
},
//...
]
};
var list = data['Episodes'];
var sel = document.getElementById('my_select');
for(var i = 0; i < list.length; i++) {
var opt = document.createElement('option');
opt.innerHTML = list[i]['Title'] + ' - Episode ' + list[i]['Episode'];
opt.value = list[i]['imdbID'];
sel.appendChild(opt);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.