如何使用JQuery将名称绑定到下拉列表中

时间:2015-10-29 09:28:28

标签: jquery json

检索JSON数据如下所示:

{
    "id": 1,
    "name": "Jon",
    "creator_id": 0,
    "date": "2015-10-26T18:58:12.62",
    "change_date": "2015-10-27T18 
    :58:12.62",
    "changer_id": 1,
    "deleted": null
},
{
    "id": 43,
    "name": "sam",
    "creator_id": 0,
    "date": "2015-10-27T16
    :36:25.45",
    "change_date": null,
    "changer_id": null,
    "deleted": null
},
{
    "id": 45,
    "name": "Alex",
    "creator_id": 0,
    "date": "2015-10-27T17:25:04.903",
    "change_date": null,
    "changer_id": null,
    "deleted": null
}

我想使用JQuery在下拉列表中绑定名称,如何做到这一点?

<div class="col-md-3">
    <select id="sel_group" name="groups" class="bs-select form-control input-medium">
        <option value="">Select Group....</option>
    </select>
</div>

1 个答案:

答案 0 :(得分:0)

您可能需要在使用前解析json。但你可以像这样追加:

$.each(json_data, function(index, obj){ // assuming json_data variable having json
    $("#sel_group").append("<option value='"+obj.id+"'>"+obj.name+"</option>")
})