请问这个问题是否已经得到回答,因为我确信它已经解决了,但是找不到我能理解的例子。

我要完成的工作是循环遍历,然后将id吐出到下拉列表中。

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));
});

最佳答案

不知道是否要向option添加更多数据,但这就是您要执行的操作。



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>

07-27 13:22