当前位置: 编程技术>jquery
JQuery级联下拉菜单实现代码
来源: 互联网 发布时间:2014-10-08
本文导语: 例子,用jquery脚本实现级联下拉菜单。 代码示例: function show_hnbInfo() { var ss = Math.random(); $.ajax( { type : 'POST', url : 'getAllHnbInfo', data : 'id=' + ss, ...
例子,用jquery脚本实现级联下拉菜单。
代码示例:
function show_hnbInfo()
{
var ss = Math.random();
$.ajax(
{
type : 'POST',
url : 'getAllHnbInfo',
data : 'id=' + ss,
success : function(msg)
{
// 清空表格
$("#internetID").empty();
// 转换成json对象
var data = JSON.parse(msg);
var option = "请选择...";
// 循环组装下拉框选项
$.each(data, function(k, v)
{
option += "" + v['hnbName'] + "";
});
$("#internetID").append(option);
$("#internetID").change(function()
{
show_freqHnbInfo($(this).val());
});
},
error : function(msg, textStatus, e)
{
alert("当前登录用户失效,请重新登录。");
window.location = path + "/login.jsp";
}
});
}
{
var ss = Math.random();
$.ajax(
{
type : 'POST',
url : 'getAllHnbInfo',
data : 'id=' + ss,
success : function(msg)
{
// 清空表格
$("#internetID").empty();
// 转换成json对象
var data = JSON.parse(msg);
var option = "请选择...";
// 循环组装下拉框选项
$.each(data, function(k, v)
{
option += "" + v['hnbName'] + "";
});
$("#internetID").append(option);
$("#internetID").change(function()
{
show_freqHnbInfo($(this).val());
});
},
error : function(msg, textStatus, e)
{
alert("当前登录用户失效,请重新登录。");
window.location = path + "/login.jsp";
}
});
}
以上是一级下拉列表框的实现,下面是二级下拉列表框的实现。
代码示例:
function show_freqHnbInfo(internetId)
{
var ss = Math.random();
$.ajax(
{
type : 'POST',
url : 'getAllHnbOnlineOutByOne',
data : 'id=' + ss + '&internetId=' + internetId,
success : function(msg)
{
// 清空表格
$("#internetId").empty();
// 转换成json对象
var data = JSON.parse(msg);
var option = "";
// 循环组装下拉框选项
$.each(data, function(k, v)
{
option += "" + v['hnbName'] + "";
});
if (internetId == "" || internetId == null || internetId == undefined)
{
option += "请选择...";
}
$("#internetId").append(option);
},
error : function(msg, textStatus, e)
{
alert("当前登录用户失效,请重新登录。");
window.location = path + "/login.jsp";
}
});
}
{
var ss = Math.random();
$.ajax(
{
type : 'POST',
url : 'getAllHnbOnlineOutByOne',
data : 'id=' + ss + '&internetId=' + internetId,
success : function(msg)
{
// 清空表格
$("#internetId").empty();
// 转换成json对象
var data = JSON.parse(msg);
var option = "";
// 循环组装下拉框选项
$.each(data, function(k, v)
{
option += "" + v['hnbName'] + "";
});
if (internetId == "" || internetId == null || internetId == undefined)
{
option += "请选择...";
}
$("#internetId").append(option);
},
error : function(msg, textStatus, e)
{
alert("当前登录用户失效,请重新登录。");
window.location = path + "/login.jsp";
}
});
}
JSP代码实现:
代码示例:
var type = "${type }";
var path = "${pageContext.request.contextPath}";
show_hnbInfo();
show_freqHnbInfos();
基站名称
请选择...
*
同频异频邻基站名称
请选择...
*
后台代码:
controller代码
代码示例:
@RequestMapping("/getAllHnbInfo")
public void getAllHnbByJosn(HttpServletRequest request, HttpServletResponse response)
{
PrintWriter out = null;
try
{
// 设置输出格式
response.setContentType("text/html");
// 页面输出对象
out = response.getWriter();
out.write(hnbService.getAllHnbByJosn());
out.flush();
out.close();
}
catch (Exception e)
{
logger.error(Global.LOG_EXCEPTION_NAME, e);
if (null != out)
{
out.write("[]");
out.flush();
out.close();
}
}
}
/**
*
*
* @param request
* @return
* @see [类、类#方法、类#成员]
*/
@RequestMapping("/getAllHnbOnlineOutByOne")
public void getAllHnbOnlineOutByOne(HttpServletRequest request, HttpServletResponse response)
{
PrintWriter out = null;
try
{
String internetId = request.getParameter("internetId");
// 设置输出格式
response.setContentType("text/html");
// 页面输出对象
out = response.getWriter();
out.write(hnbService.getAllHnbOnlineOutByOne(internetId));
out.flush();
out.close();
}
catch (Exception e)
{
logger.error(Global.LOG_EXCEPTION_NAME, e);
if (null != out)
{
out.write("[]");
out.flush();
out.close();
}
}
}
}
public void getAllHnbByJosn(HttpServletRequest request, HttpServletResponse response)
{
PrintWriter out = null;
try
{
// 设置输出格式
response.setContentType("text/html");
// 页面输出对象
out = response.getWriter();
out.write(hnbService.getAllHnbByJosn());
out.flush();
out.close();
}
catch (Exception e)
{
logger.error(Global.LOG_EXCEPTION_NAME, e);
if (null != out)
{
out.write("[]");
out.flush();
out.close();
}
}
}
/**
*
*
* @param request
* @return
* @see [类、类#方法、类#成员]
*/
@RequestMapping("/getAllHnbOnlineOutByOne")
public void getAllHnbOnlineOutByOne(HttpServletRequest request, HttpServletResponse response)
{
PrintWriter out = null;
try
{
String internetId = request.getParameter("internetId");
// 设置输出格式
response.setContentType("text/html");
// 页面输出对象
out = response.getWriter();
out.write(hnbService.getAllHnbOnlineOutByOne(internetId));
out.flush();
out.close();
}
catch (Exception e)
{
logger.error(Global.LOG_EXCEPTION_NAME, e);
if (null != out)
{
out.write("[]");
out.flush();
out.close();
}
}
}
}
service代码:
代码示例:
@Override
public String getAllHnbByJosn()
{
List hnbList = getHnbOnlineList();
if (null != hnbList)
{
StringBuffer sb = new StringBuffer();
sb.append("[");
//得到集合的长度
int size = hnbList.size();
int i = 0;
for (HnbInfo hnbInfo : hnbList)
{
i++;
String internetID = hnbInfo.getInternetID();
String hnbIdentifier = hnbInfo.getHnbIdentifier();
String hnbName = hnbInfo.getHnbName();
sb.append("{"internetID":"");
sb.append(internetID);
sb.append("","hnbIdentifier":"");
sb.append(hnbIdentifier);
sb.append("","hnbName":"");
sb.append(hnbName);
sb.append(""}");
//如果i小于size字符串sb中加","
if (i < size)
{
sb.append(",");
}
}
sb.append("]");
//拼好的字符串赋值给变量
return sb.toString();
}
else
{
return "[]";
}
}
@Override
public String getAllHnbOnlineOutByOne(String internetId)
{
// 根据基站ID 获取基站信息
InternetGatewayDevice internet = hnbDao.getOneHnbByInternetID(internetId);
if (null != internet && !Global.isEmpty(internet.getApIpAdress()))
{
// 根据基站注册IP地址获取所有除基站注册IP地址对应的基站外的所有基站
List hnblist = getAllHnbOutByone(internet.getApIpAdress());
if (null != hnblist && hnblist.size() != 0)
{
StringBuffer sb = new StringBuffer();
sb.append("[");
//得到集合的长度
int size = hnblist.size();
int i = 0;
for (HnbInfo hnbInfo : hnblist)
{
i++;
String internetID = hnbInfo.getInternetID();
String hnbIdentifier = hnbInfo.getHnbIdentifier();
String hnbName = hnbInfo.getHnbName();
sb.append("{"internetID":"");
sb.append(internetID);
sb.append("","hnbIdentifier":"");
sb.append(hnbIdentifier);
sb.append("","hnbName":"");
sb.append(hnbName);
sb.append(""}");
//如果i小于size字符串sb中加","
if (i < size)
{
sb.append(",");
}
}
sb.append("]");
//拼好的字符串赋值给变量
return sb.toString();
}
else
{
return "[]";
}
}
else
{
return "[]";
}
}
public String getAllHnbByJosn()
{
List hnbList = getHnbOnlineList();
if (null != hnbList)
{
StringBuffer sb = new StringBuffer();
sb.append("[");
//得到集合的长度
int size = hnbList.size();
int i = 0;
for (HnbInfo hnbInfo : hnbList)
{
i++;
String internetID = hnbInfo.getInternetID();
String hnbIdentifier = hnbInfo.getHnbIdentifier();
String hnbName = hnbInfo.getHnbName();
sb.append("{"internetID":"");
sb.append(internetID);
sb.append("","hnbIdentifier":"");
sb.append(hnbIdentifier);
sb.append("","hnbName":"");
sb.append(hnbName);
sb.append(""}");
//如果i小于size字符串sb中加","
if (i < size)
{
sb.append(",");
}
}
sb.append("]");
//拼好的字符串赋值给变量
return sb.toString();
}
else
{
return "[]";
}
}
@Override
public String getAllHnbOnlineOutByOne(String internetId)
{
// 根据基站ID 获取基站信息
InternetGatewayDevice internet = hnbDao.getOneHnbByInternetID(internetId);
if (null != internet && !Global.isEmpty(internet.getApIpAdress()))
{
// 根据基站注册IP地址获取所有除基站注册IP地址对应的基站外的所有基站
List hnblist = getAllHnbOutByone(internet.getApIpAdress());
if (null != hnblist && hnblist.size() != 0)
{
StringBuffer sb = new StringBuffer();
sb.append("[");
//得到集合的长度
int size = hnblist.size();
int i = 0;
for (HnbInfo hnbInfo : hnblist)
{
i++;
String internetID = hnbInfo.getInternetID();
String hnbIdentifier = hnbInfo.getHnbIdentifier();
String hnbName = hnbInfo.getHnbName();
sb.append("{"internetID":"");
sb.append(internetID);
sb.append("","hnbIdentifier":"");
sb.append(hnbIdentifier);
sb.append("","hnbName":"");
sb.append(hnbName);
sb.append(""}");
//如果i小于size字符串sb中加","
if (i < size)
{
sb.append(",");
}
}
sb.append("]");
//拼好的字符串赋值给变量
return sb.toString();
}
else
{
return "[]";
}
}
else
{
return "[]";
}
}