欢迎来到福编程网,本站提供各种互联网专业知识!

Ajax二级联动菜单实现原理及代码

发布时间:2013-01-30 作者: 来源:转载
二级联动菜单,一个多么方便且实用的用户交互功能,有需求的朋友可以参考下本文,或许对你的菜单知识深入学习有帮助,好了,闲话不多说,看代码吧
index.jsp:
复制代码 代码如下:
<%@ page language="java" pageEncoding="UTF-8"%>


二级菜单联动演示
















二级联动示例

<select name="province" id="province" onChange="Change_Select()">






<select name="city" id="city">










SelectServlet:
复制代码 代码如下:
package com;
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 = "";
String xml = "";
if (targetId.equalsIgnoreCase("0"))
{
xml = "<select>0请选择";
} else if (targetId.equalsIgnoreCase("1"))
{
xml = "<select>1昌平";
xml += "<select>2丰台";
xml += "<select>3海淀";
xml += "<select>4朝阳";
} else if (targetId.equalsIgnoreCase("2"))
{
xml = "<select>1塘沽区";
xml += "<select>2汉沽区";
xml += "<select>3大港区";
xml += "<select>4东丽区";
} else
{// 如果是3,则返回下面的字符
xml = "<select>1济南";
xml += "<select>2青岛";
xml += "<select>3淄博";
xml += "<select>4枣庄";
}
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:
复制代码 代码如下:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

SelectServlet
com.SelectServlet


SelectServlet
/select


index.jsp


相关推荐