如何使用ajax在select下拉列表中填充json数据
how to populate the json data in select dropdown using ajax?
实际上,我需要使用select标签在下拉列表中显示学校列表,到目前为止,我正在通过硬编码值获得响应,现在这里是无法通过链接生成的问题,我正在从rest全方位服务中获取数据如何做到这一点,请帮助
<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>
您可以使用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>');
});
});
});
试试这个,
$(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);
}
});
}
相关文章:
- 用我的json数据填充JQuery DataTable
- 如何使用JSON字符串中的jQuery填充下拉框
- 从JSON填充列表(而不是HTML)
- 如何在 Jquery 中填充 JSON 响应值
- 如何在不丢失浮点精度的情况下填充 JSON 数据
- 用字符串和字符串数组动态填充json对象
- 如何使用ajax在select下拉列表中填充json数据
- 填充JSON数组
- 需要从 Angular JS 应用程序中的另一个 JSON 对象数组填充 JSON 数组的每个对象 JSON 对象数组
- 创建和填充json对象
- Angularjs -从输入文本ng-model填充json数组
- 用嵌套的对象数组填充JSON对象的正确格式是什么?
- 动态填充JSON层次结构
- 无法用正确格式的数据填充JSON请求
- 我如何从模板节点复制并填充json数据,并用javascript将它们附加到文档中
- 我如何加载填充JSON作为一个不可执行的,例如字符串或数据像常规JSON
- 胡子填充JSON对象错误
- 如何动态填充json数组对象
- 如何使用角度填充 json 数据
- 填充JSON动态数组