<script type="text/javascript">
//显示联动菜单
//初始化
$.ajax({
url: "showIllegalSelectOption",//调用方法
type: "GET",
data: {id:"0"},//参数
dataType: "json",//类型
success: function(data) {
var tbHtml = "";//定义HTML
tbHtml += "<option></option>";
$.each(data, function(key, value) {//循环
tbHtml+="<option value="+value.id+">"+value.name+"</option>"//显示查询出来的结果内容
});
$('#ddlCompany').html(tbHtml);//显示HTML
}
});
$(document).ready(function() {
$('#ddlCompany').change(function() {//变更事件
GetData();//获取第二个select数据
});
});
function GetData() {
var id = $('#ddlCompany').val();
$.ajax({
url: "showIllegalSelectOption",
type: "GET",
data: {id : $("#ddlCompany").val()},
dataType: "json",
success: function(data) {
$("#iid").empty();//清空
$("#iid").show();//显示select
var secondHtml = "";
secondHtml += "<option></option>";
$.each(data, function(key, value) {
secondHtml+="<option value="+value.id+">"+value.name+"</option>"
});
$("#iid").html(secondHtml);
}
});
}
</script>
html页面:两个select,第一个默认显示,第二个默认隐藏
<div ID="ddlSummary">
<select id="ddlCompany">
</select>
<select id="iid" style="display:none">
</select>
</div>
java
//显示illegal表中的下拉菜单,通过调用数据库返回LIST
@RequestMapping(value="/showIllegalSelectOption", method=RequestMethod.GET)
public @ResponseBody Object showIllegalSelectOption(HttpServletRequest request,HttpServletResponse response) {
String id = request.getParameter("id");
//Object... obj
List<Object> paraes = new ArrayList();
String hql = "from Illegal wc where 1=1 ";
if(id != null && !id.equals(""))
{
hql += "and supername=? ";
paraes.add(id);
}
List<Illegal> queryResult=null;
if(paraes.size() <= 0)
{
queryResult=illegalService.queryIllegal(hql);
}
else if(paraes.size() == 1)
{
queryResult=illegalService.queryIllegal(hql, paraes.get(0));
}
else
{
queryResult=illegalService.queryIllegal(hql, paraes.get(0), paraes.get(1));
}
return queryResult;
}