[英]how to populate the json data in select dropdown using ajax?
Actually i need to display the list of school in a dropdown using select tag so far i am getting the response through hard coded values now here is the problem not able to generate through a link i am getting the data from rest full service how to do that , any on please help 实际上,到目前为止,我需要使用选择标记在下拉列表中显示学校列表。我现在通过硬编码值获取响应,现在这里是无法通过链接生成的问题,我正在从其余全部服务获取数据。那,请帮忙
<html>
<head>
<meta http-equiv="content-type" content="application/json; charset=UTF-8">
</head>
<body>
<select id="sel"></select>
<script>
$(function() {
var data = [
{
"id": "1",
"name": "test1"},
{
"id": "2",
"name": "test2"}
];
$.each(data, function(i, option) {
$('#sel').append($('<option/>').attr("value", option.id).text(option.name));
});
})
</script>
</body>
</html>
You can use the getJSON method of jQuery 您可以使用jQuery的getJSON方法
$('#brand').change(function(){
$.getJSON(
'your url to get json string',
'get parameters to send if any',
function(result){
//result would have your json string
//Empty the dropdown if it is having some items
$('#item').empty();
//Looping through all the json items
$.each(result.result, function(){
//Appending the json items to the dropdown (select tag)
//item is the id of your select tag
$('#item').append('<option>'+this['item']+'</option>');
});
});
});
Try this, 尝试这个,
$(document).ready(function(){
getschool(val);
});
function getschool(val) {
$.ajax({
type: "GET",
url: 'http://localhost:8080/SMWS/Rest/parentService/parent/getSchoolDetails',
contentType: "application/json;charset=utf-8",
dataType: "json",
data:'school_id='+val,
success: function(data){
var html = '';
$.each(data, function(index,value){
html+= '<option value="'+value['item_value']+'">'+value['item']+'</option>';
});
$('#country-list').html(html);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.