JavaScript之AJAX实现二级联动菜单(jsp+js+servlet+xml)

时间:2021-12-17 11:49:22

1、首先给出实现此功能的项目文件部署图(MyEclipse上实现)

JavaScript之AJAX实现二级联动菜单(jsp+js+servlet+xml)

2、实现三个文件

2.1、index.jsp源码如下:

<%@ page language="java" pageEncoding="UTF-8"%> 
<html> 
<head> 
	<title>二级菜单联动演示</title> 
	<script type="text/javascript" src="js/1.js"></script>
</head> 
<body> 
	<div align="center"> 
		<h1>二级联动示例 </h1>
        <!--第一个下拉菜单--> 
		<select name="province" id="province" onChange="changeSelect()"> 
			<option value="0"> 请选择武侠大师 </option> 
			<option value="1"> 金庸</option> 
			<option value="2"> 古龙 </option> 
			<option value="3"> 梁羽生</option> 
		</select> 

		<!--第二个下拉菜单-->
		<select name="city" id="city"> 
 			<option value="0"> 请选择大师作品</option> 
		</select>
 
	</div> 
</body> 
</html> 

2.2、1.js文件源码如下:

var xhr; 
//根据不同浏览器创建xhr对象的函数
function creatXHR () {
	if (typeof(XMLHttpRequest)!="undefined") {     
		return new XMLHttpRequest();         //针对IE7及四大浏览器
	}else if (typeof ActiveXObject !="undefined") {
		if (typeof arguments.callee.activeXString!="string") {
			var versions=["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"];
			for (var i=0,len=versions.length;i<len;i++) {
				try{
					var xhr = new ActiveXObject(versions[i]);//针对IE7以下版本
					arguments.callee.activeXString=versions[i];
					return xhr;
				}catch(e){
					//TODO handle the exception
				}
			}
		}
		return new ActiveXObject(arguments.callee.activeXString);
	 }else{
	 	throw new Error("No XHR object available");
	 }
}
//当第一个下拉框的选项发生改变时调用该函数 
function changeSelect(){
	var province = document.getElementById('province').value; 
	var url = "select?id="+ encodeURI(province); 
	xhr=creatXHR();
	if(xhr){ 
		xhr.open("GET",url,true); 
		//指定回调函数为callback 
		xhr.onreadystatechange = callback; 
		xhr.send(null); 
	} 
} 
//回调函数 
function callback(){ 
	if (xhr.readyState==4) {
		  if ((xhr.status>=200&&xhr.status<300)||xhr.status==304) {
			  parseMessage();
		  }else{
		     alert("Request was unsuccessful:"+xhr.status);
		  }
	}
} 
//解析返回xml的函数
function parseMessage(){ 
	var xmlDoc = xhr.responseXML.documentElement;//获得返回的XML文档 
	var xSel = xmlDoc.getElementsByTagName('select'); //获得XML文档中的所有<select>标记 
	var select_root = document.getElementById('city'); //获得网页中的第二个下拉框 
	select_root.options.length=0; //每次获得新的数据的时候先把每二个下拉框架的长度清0 
	for(var i=0;i<xSel.length;i++){ 
		var xValue = xSel[i].childNodes[0].firstChild.nodeValue; //获得每个<select>标记中的第一个标记的值,也就是<value>标记的值 
		var xText = xSel[i].childNodes[1].firstChild.nodeValue; //获得每个<select>标记中的第二个标记的值,也就是<text>标记的值 
		var option = new Option(xText, xValue); //根据每组value和text标记的值创建一个option对象  
		select_root.add(option);//将option对象添加到第二个下拉框中 
	} 
} 

2.3、SelectServlet.java文件源码

package Servlet;

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class SelectServlet extends HttpServlet { 
	private static final long serialVersionUID = 1L; 
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException 
			{ 
				response.setContentType("text/xml"); 
				response.setHeader("Cache-Control", "no-cache"); 
				request.setCharacterEncoding("GBK"); 
				response.setCharacterEncoding("UTF-8"); 
				String targetId = request.getParameter("id").toString(); //获得武侠大师编号
				String xml_start = "<selects>"; 
				String xml_end = "</selects>"; 
				String xml = ""; 
				if (targetId.equalsIgnoreCase("0")) 
				{ 
				xml = "<select><value>0</value><text>请选择大师作品</text></select>"; 
				} else if (targetId.equalsIgnoreCase("1")) 
				{ 
				xml = "<select><value>1</value><text>射雕英雄传</text></select>"; 
				xml += "<select><value>2</value><text>神雕侠侣</text></select>"; 
				xml += "<select><value>3</value><text>天龙八部</text></select>"; 
				xml += "<select><value>4</value><text>倚天屠龙记</text></select>"; 
				} else if (targetId.equalsIgnoreCase("2")) 
				{ 
				xml = "<select><value>1</value><text>小李飞刀</text></select>"; 
				xml += "<select><value>2</value><text>飞刀问情</text></select>"; 
				xml += "<select><value>3</value><text>飞刀又见飞刀</text></select>"; 
				xml += "<select><value>4</value><text>萧十一郎</text></select>"; 
				} else 
				{// 如果是3,则返回下面的字符 
				xml = "<select><value>1</value><text>云海玉弓缘</text></select>"; 
				xml += "<select><value>2</value><text>萍踪侠影</text></select>"; 
				xml += "<select><value>3</value><text>白发魔女传</text></select>"; 
				xml += "<select><value>4</value><text>七剑下天山</text></select>"; 
				} 
				String last_xml = xml_start + xml + xml_end; 
				response.getWriter().write(last_xml); 
			} 
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException 
			{ 
				doGet(request, response); 
			} 
} 

3、效果图

JavaScript之AJAX实现二级联动菜单(jsp+js+servlet+xml)

JavaScript之AJAX实现二级联动菜单(jsp+js+servlet+xml)