Ajax二级联动菜单实现原理及代码
网络编程 2021-07-05 10:37www.168986.cn编程入门
二级联动菜单,一个多么方便且实用的用户交互功能,有需求的朋友可以参考下本文,或许对你的菜单知识深入学习有帮助,好了,闲话不多说,看代码吧
index.jsp:
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>二级菜单联动演示</title>
<script type="text/javascript">
var req;
window.onload=function()
{//页面加载时的函数
}
function Change_Select(){//当第一个下拉框的选项发生改变时调用该函数
var province = document.getElementById('province').value;
var url = "select?id="+ escape(province);
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
}else if(window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url,true);
//指定回调函数为callback
req.onreadystatechange = callback;
req.send(null);
}
}
//回调函数
function callback(){
if(req.readyState ==4){
if(req.status ==200){
parseMessage();//解析XML文档
}else{
alert("不能得到描述信息:" + req.statusText);
}
}
}
//解析返回xml的方法
function parseMessage(){
var xmlDoc = req.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对象
try{
select_root.add(option);//将option对象添加到第二个下拉框中
}catch(e){
}
}
}
</script>
</head>
<body>
<div align="center">
<form name="form1" method="post" action="">
<table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
二级联动示例
</td>
</tr>
<tr>
<td>
<select name="province" id="province" onChange="Change_Select()">
<!--第一个下拉菜单-->
<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>
</td>
</tr>
<tr>
<td>
</td>
<tr>
</table>
</form>
</div>
</body>
</html>
SelectServlet:
package ;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/
@author zdw
/
public class SelectServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public SelectServlet()
{
super();
}
public void destroy()
{
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// response.setCharacterEncoding("GBK");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("UTF-8");
String targetId = request.getParameter("id").toString();
System.out.println(targetId);
// 获得请求中参数为id的值
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,则返回狼蚁网站SEO优化的字符
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);
}
public void init() throws ServletException
{
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun./xml/ns/j2ee"
xmlns:xsi="http://.w3./2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun./xml/ns/j2ee
http://java.sun./xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>.SelectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectServlet</servlet-name>
<url-pattern>/select</url-pattern>
</servlet-mapping>
<wele-file-list>
<wele-file>index.jsp</wele-file>
</wele-file-list>
</web-app>
代码如下:
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>二级菜单联动演示</title>
<script type="text/javascript">
var req;
window.onload=function()
{//页面加载时的函数
}
function Change_Select(){//当第一个下拉框的选项发生改变时调用该函数
var province = document.getElementById('province').value;
var url = "select?id="+ escape(province);
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
}else if(window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url,true);
//指定回调函数为callback
req.onreadystatechange = callback;
req.send(null);
}
}
//回调函数
function callback(){
if(req.readyState ==4){
if(req.status ==200){
parseMessage();//解析XML文档
}else{
alert("不能得到描述信息:" + req.statusText);
}
}
}
//解析返回xml的方法
function parseMessage(){
var xmlDoc = req.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对象
try{
select_root.add(option);//将option对象添加到第二个下拉框中
}catch(e){
}
}
}
</script>
</head>
<body>
<div align="center">
<form name="form1" method="post" action="">
<table width="70%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
二级联动示例
</td>
</tr>
<tr>
<td>
<select name="province" id="province" onChange="Change_Select()">
<!--第一个下拉菜单-->
<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>
</td>
</tr>
<tr>
<td>
</td>
<tr>
</table>
</form>
</div>
</body>
</html>
SelectServlet:
代码如下:
package ;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/
@author zdw
/
public class SelectServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public SelectServlet()
{
super();
}
public void destroy()
{
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// response.setCharacterEncoding("GBK");
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("UTF-8");
String targetId = request.getParameter("id").toString();
System.out.println(targetId);
// 获得请求中参数为id的值
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,则返回狼蚁网站SEO优化的字符
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);
}
public void init() throws ServletException
{
}
}
web.xml:
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun./xml/ns/j2ee"
xmlns:xsi="http://.w3./2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun./xml/ns/j2ee
http://java.sun./xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>SelectServlet</servlet-name>
<servlet-class>.SelectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SelectServlet</servlet-name>
<url-pattern>/select</url-pattern>
</servlet-mapping>
<wele-file-list>
<wele-file>index.jsp</wele-file>
</wele-file-list>
</web-app>
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程