我使用$ .getJSON,它返回json数据。
{
"sEcho": 1,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
{
"student": {
"id": "1",
"name": "gaurav"
}
},
{
"student": {
"id": "2",
"name": "abhishek"
}
}
]
}
我想将学生ID和姓名放入select选项值。 Html会是这样的:
<select name='stud'>
<option id='1'>gaurav</option>
<option id='1'>abhishek</option>
</select>
当我使用JSON.stringify时,它返回对象对象。 提前谢谢。
答案 0 :(得分:0)
这就是你追求的吗? (假设您在页面上使用jQuery):
<script type="text/javascript">
var json = {
"sEcho": 1,
"iTotalRecords": 2,
"iTotalDisplayRecords": 2,
"aaData": [
{
"student": {
"id": "1",
"name": "gaurav"
}
}, {
"student": {
"id": "2",
"name": "abhishek"
}
}
]
};
json.aaData.forEach(function(value) {
var student = value.student
$('#mySelect')
.append($("<option></option>")
.attr("value",student.id)
.text(student.name));
});
</script>
<select id='mySelect' name='stud'>
</select>
请参阅此处的工作示例:http://jsfiddle.net/r5nn5wqu/
答案 1 :(得分:0)
$.getJSON( "ajax/test.json", function( data ) {
var items = [];
$.each( data, function( key, val ) {
$('select[name="stud"]').append('<option id="'+key+'" value="'+value+'"');
});
}