您的位置:首页 > 数据库

select下拉选项调用另一张数据库表数据

2016-01-31 10:25 459 查看
页面展示,一个下拉框,如下:

<tr>
<td style="padding:8px;"><label for="pathTypeId">链路类型:</label></td>
<td>
<select name="pathTypeId.id" id="pathTypeId" style="width:140px;" >
</select>
</td>

</tr>


确定id ,通过id 调用ajax 方法,js源码如下:
<script >
$(function(){
// alert(123);
initSelect();

})
function initSelect(){
$.ajax({
url:'${basePath}/admin/pathType/getAllData',
type:'get',
data:'json',
success:function(res){
res=JSON.parse(res);  //JSON转换为对象
for(var i=0;i<res.length;i++){
console.log(res[i]);
$("#pathTypeId").append("<option value='"+ res[i].id +"'>"+ res[i].name +"</option>");
}
},
error:function(){
alert("pathTypeId init fail");
}

});

}

</script>


function的初始化方法:

$(function(){
// alert(123);
initSelect();

})
url获取数据得到一个对象,controller层方法如下:

@RequestMapping(value = "getAllData")
@ResponseBody
public List<Path> findAll(){
List<Path> AllInfo = pathBiz.findAll();
return AllInfo;
}


select下拉选项for循环遍历,根据id获得velue:

$("#pathTypeId").append("<option value='"+ res[i].id +"'>"+ res[i].name +"</option>");


编辑页可用同种方法,but方法名不可以相同,id也不相同

更多有关jquery信息http://www.w3school.com.cn/jquery/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: