如果这个问题已经得到解答,请原谅,因为我确信它已经得到解答,但找不到我理解的示例。
我想要完成的是循环并将 ids 吐出到下拉列表中。
const things = {
"list": [{
"id": "123456",
"name": "Jack Smith"
},
{
"id": "789101",
"name": "Ellie Cocks"
},
{
"id": "535253",
"name": "Peter Johns"
},
{
"id": "9353638",
"name": "Anna Jacobs"
}
]}
$.each(things, function() {
$('#dropdown').append($("<option />").text(things.list.id));
});
最佳答案
不确定您是否希望将更多数据添加到选项
,但这就是您想要的。
const select = $('#dropdown');
const things = {
"list": [{
"id": "123456",
"name": "Jack Smith"
},
{
"id": "789101",
"name": "Ellie Cocks"
},
{
"id": "535253",
"name": "Peter Johns"
},
{
"id": "9353638",
"name": "Anna Jacobs"
}
]
}
$.each(things.list, (i, item) => {
let opt = $("<option />", {
value: item.id,
text: item.name
});
select.append(opt);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<select id="dropdown"></select>
关于javascript - 循环对象内部的数组并将 id 吐出到下拉列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48137203/