Ajax 实现级联菜单

自己动手实现的一个Ajax级联菜单,开发平台:Eclipse,数据库:MySQL。数据库设计如下图所示:

 

 

 

1. 前台实现:

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*,java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ajax级联菜单实现</title>
<script language="JavaScript" type="text/javascript">
<!--
var cache = [];
function getLevel2(){
	if(document.forms.LevelMenu.select1.selectedIndex == 0){
		//当一级菜单未被选中时,二级菜单保持初始状态
		document.forms.LevelMenu.select2.length = 1;
		return;
	}
	//如果当前二级分类没有缓存时,则从服务器获取数据
	if(!cache[document.forms.LevelMenu.select1.selectedIndex]){
		//创建跨浏览器的XMLHttpRequest对象
	var xmlhttp;
	try{
	//IE 5.0 
		xmlhttp = new ActiveXObject('Msxm12.XMLHTTP');
	}catch(e){
		try{
		//IE 5.5 及更高版本
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		}catch(e){
			try{
			//其他浏览器
				xmlhttp = new XMLHttpRequest();
			}catch(e){}
		}
	}
	xmlhttp.open("get","ShowTwoMenu.jsp?id="+document.forms.LevelMenu.select1.value);
	xmlhttp.onreadystatechange = function(){
		if(xmlhttp.readyState == 4){
			if(xmlhttp.status == 200){
				cache[document.forms.LevelMenu.select1.selectedIndex] = eval("("+xmlhttp.responseText+")");
				getLevel2();
			}
		}
	}
	xmlhttp.send(null);
	return;
	}
	document.forms.LevelMenu.select2.length = 1;
	var _arr = cache[document.forms.LevelMenu.select1.selectedIndex];
	for (var i=0; i<_arr.length-1; i+=2){
		with(document.forms.LevelMenu.select2){
			options[options.length] = new Option(_arr[i], _arr[i+1]);
		}
	}
}
//-->
</script>
</head>
<body>
<%
	//驱动器名
	//数据库用户名和密码
	String userName = "root";
	String userPasswd = "1984428";
	//需要连接的数据库名
	String dbName = "studyajax";
	//表名
	String tableName = "articleType";
	//创建连接并执行查询操作
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	Connection conn = DriverManager.getConnection(
			"jdbc:mysql://localhost:3306/" + dbName, userName,
			userPasswd);
	Statement statement = conn.createStatement();
	String sql = "SELECT * FROM " + tableName + " where level=0";
	ResultSet rs = statement.executeQuery(sql);
%>
<form name="LevelMenu"><select name="select1" id="select1"
	οnchange="getLevel2()">
	<option value="0">请选择一级分类:</option>
	<%
		while (rs.next()) {
	%>
	<option value="<%=rs.getInt(1) %>"><%=rs.getString(2)%></option>
	<%
		}
		rs.close();
		statement.close();
		conn.close();
	%>
</select> <select name="select2" id="select2">
	<option value="0">请选择二级分类</option>
</select></form>

</body>
</html>

 

2. 后台代码实现

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*,java.io.*"%>
<%
	//数据库用户名和密码
	int id = Integer.parseInt(request.getParameter("id").trim());
	//System.out.println("id:"+id);
	String userName = "root";
	String userPasswd = "1984428";
	//需要连接的数据库名
	String dbName = "studyajax";
	//表名
	String tableName = "articletype";
	//创建连接并执行查询操作
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	Connection conn = DriverManager.getConnection(
			"jdbc:mysql://localhost:3306/" + dbName, userName,
			userPasswd);
	Statement statement = conn.createStatement();
	String sql = "SELECT id, name FROM " + tableName
			+ "  where level=1 and parentId=" + id;
	//System.out.println("sql:"+sql);
	ResultSet rs = statement.executeQuery(sql);

	//获取数据结果集
	response.setContentType("text/html; charset=UTF-8");
	response.setHeader("Cache-Control", "no-cache");
	PrintWriter pout = null;
	pout = response.getWriter();
	pout.print("[");
	while (rs.next()) {
		try {

			pout.print("'" + (rs.getString("name")) + "',");
			pout.print(rs.getString("id"));
			pout.print(",");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	pout.print("0]");
	rs.close();
	statement.close();
	conn.close();
%>
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值