Ajax實現二級聯動菜單

http://www.blogjava.net/supercrsky/articles/203698.html javascript


index.jsp:
html

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ page language="java" pageEncoding="UTF-8" %>
None.gif < html >
None.gif < head >
None.gif < title >二級菜單聯動演示 </ title >
ExpandedBlockStart.gif ContractedBlock.gif < script type ="text/javascript" > dot.gif
InBlock.gif
var req;
InBlock.gif window.onload
=function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{//頁面加載時的函數
ExpandedSubBlockEnd.gif
}

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
function Change_Select()dot.gif{//當第一個下拉框的選項發生改變時調用該函數
InBlock.gif
var province = document.getElementById('province').value;
InBlock.gif
var url ="select?id="+ escape(province);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(window.XMLHttpRequest)dot.gif{
InBlock.gif req
=new XMLHttpRequest();
ExpandedSubBlockStart.gifContractedSubBlock.gif }
elseif(window.ActiveXObject)dot.gif{
InBlock.gif req
=new ActiveXObject("Microsoft.XMLHTTP");
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif
if(req)dot.gif{
InBlock.gif req.open(
"GET",url,true);
InBlock.gif
//指定回調函數爲callback
InBlock.gif
req.onreadystatechange = callback;
InBlock.gif req.send(
null);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
//回調函數
ExpandedSubBlockStart.gifContractedSubBlock.gif
function callback()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(req.readyState ==4)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(req.status ==200)dot.gif{
InBlock.gif parseMessage();
//解析XML文檔
ExpandedSubBlockStart.gifContractedSubBlock.gif
}
elsedot.gif{
InBlock.gif alert(
"不能獲得描述信息:"+ req.statusText);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

InBlock.gif
//解析返回xml的方法
ExpandedSubBlockStart.gifContractedSubBlock.gif
function parseMessage()dot.gif{
InBlock.gif
var xmlDoc = req.responseXML.documentElement;//得到返回的XML文檔
InBlock.gif
var xSel = xmlDoc.getElementsByTagName('select');
InBlock.gif
//得到XML文檔中的全部<select>標記
InBlock.gif
var select_root = document.getElementById('city');
InBlock.gif
//得到網頁中的第二個下拉框
InBlock.gif
select_root.options.length=0;
InBlock.gif
//每次得到新的數據的時候先把每二個下拉框架的長度清0
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
for(var i=0;i<xSel.length;i++)dot.gif{
InBlock.gif
var xValue = xSel[i].childNodes[0].firstChild.nodeValue;
InBlock.gif
//得到每一個<select>標記中的第一個標記的值,也就是<value>標記的值
InBlock.gif
var xText = xSel[i].childNodes[1].firstChild.nodeValue;
InBlock.gif
//得到每一個<select>標記中的第二個標記的值,也就是<text>標記的值
InBlock.gif

InBlock.gif
var option =new Option(xText, xValue);
InBlock.gif
//根據每組value和text標記的值建立一個option對象
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif
trydot.gif{
InBlock.gif select_root.add(option);
//將option對象添加到第二個下拉框中
ExpandedSubBlockStart.gifContractedSubBlock.gif
}
catch(e)dot.gif{
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif
</ script >
None.gif </ head >
None.gif
None.gif < body >
None.gif < div align ="center" >
None.gif < form name ="form1" method ="post" action ="" >
None.gif < table width ="70%" border ="0" cellspacing ="0" cellpadding ="0" >
None.gif < tr >
None.gif < td align ="center" >
None.gif 二級聯動示例
None.gif </ td >
None.gif </ tr >
None.gif < tr >
None.gif < td >
None.gif < select name ="province" id ="province" onChange ="Change_Select()" >
None.gif <!-- 第一個下拉菜單 -->
None.gif < option value ="0" >
None.gif 請選擇
None.gif </ option >
None.gif < option value ="1" >
None.gif 北京
None.gif </ option >
None.gif < option value ="2" >
None.gif 天津
None.gif </ option >
None.gif < option value ="3" >
None.gif 山東
None.gif </ option >
None.gif </ select >
None.gif < select name ="city" id ="city" >
None.gif <!-- 第二個下拉菜單 -->
None.gif < option value ="0" >
None.gif 請選擇
None.gif </ option >
None.gif </ select >
None.gif </ td >
None.gif </ tr >
None.gif < tr >
None.gif < td >
None.gif </ td >
None.gif < tr >
None.gif </ table >
None.gif </ form >
None.gif </ div >
None.gif </ body >
None.gif </ html >
None.gif

SelectServlet:
None.gif package com;
None.gif
None.gif import java.io.IOException;
None.gif
None.gif import javax.servlet.ServletException;
None.gif import javax.servlet.http.HttpServlet;
None.gif import javax.servlet.http.HttpServletRequest;
None.gif import javax.servlet.http.HttpServletResponse;
ExpandedBlockStart.gif ContractedBlock.gif /** */ /***
InBlock.gif *
InBlock.gif *
@author zdw
InBlock.gif *
ExpandedBlockEnd.gif
*/

None.gif public class SelectServlet extends HttpServlet
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
InBlock.gif
InBlock.gifprivatestaticfinallong serialVersionUID = 1L;
InBlock.gif
InBlock.gifpublic SelectServlet()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifsuper();
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublicvoid destroy()
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gifsuper.destroy();
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublicvoid doGet(HttpServletRequest request, HttpServletResponse response)
InBlock.gifthrows ServletException, IOException
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif// response.setCharacterEncoding("GBK");
InBlock.gif
response.setContentType("text/xml");
InBlock.gif response.setHeader("Cache-Control", "no-cache");
InBlock.gif request.setCharacterEncoding("GBK");
InBlock.gif response.setCharacterEncoding("UTF-8");
InBlock.gif String targetId = request.getParameter("id").toString();
InBlock.gif System.out.println(targetId);
InBlock.gif// 得到請求中參數爲id的值
InBlock.gif
String xml_start = "<selects>";
InBlock.gif String xml_end = "</selects>";
InBlock.gif String xml = "";
InBlock.gif
InBlock.gifif (targetId.equalsIgnoreCase("0"))
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif xml = "<select><value>0</value><text>請選擇</text></select>";
ExpandedSubBlockEnd.gif }
elseif (targetId.equalsIgnoreCase("1"))
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif xml = "<select><value>1</value><text>昌平</text></select>";
InBlock.gif xml += "<select><value>2</value><text>豐臺</text></select>";
InBlock.gif xml += "<select><value>3</value><text>海淀</text></select>";
InBlock.gif xml += "<select><value>4</value><text>朝陽</text></select>";
ExpandedSubBlockEnd.gif }
elseif (targetId.equalsIgnoreCase("2"))
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif xml = "<select><value>1</value><text>塘沽區</text></select>";
InBlock.gif xml += "<select><value>2</value><text>漢沽區</text></select>";
InBlock.gif xml += "<select><value>3</value><text>大港區</text></select>";
InBlock.gif xml += "<select><value>4</value><text>東麗區</text></select>";
ExpandedSubBlockEnd.gif }
else
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{// 若是是3,則返回下面的字符
InBlock.gif
xml = "<select><value>1</value><text>濟南</text></select>";
InBlock.gif xml += "<select><value>2</value><text>青島</text></select>";
InBlock.gif xml += "<select><value>3</value><text>淄博</text></select>";
InBlock.gif xml += "<select><value>4</value><text>棗莊</text></select>";
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif String last_xml = xml_start + xml + xml_end;
InBlock.gif response.getWriter().write(last_xml);
InBlock.gif
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublicvoid doPost(HttpServletRequest request, HttpServletResponse response)
InBlock.gifthrows ServletException, IOException
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
InBlock.gif doGet(request, response);
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gifpublicvoid init() throws ServletException
ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

web.xml:
None.gif <? xml version="1.0" encoding="UTF-8" ?>
None.gif < web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee"
None.gif xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
None.gif xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee
None.gif http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
None.gif < servlet >
None.gif < servlet-name >SelectServlet </ servlet-name >
None.gif < servlet-class >com.SelectServlet </ servlet-class >
None.gif </ servlet >
None.gif
None.gif < servlet-mapping >
None.gif < servlet-name >SelectServlet </ servlet-name >
None.gif < url-pattern >/select </ url-pattern >
None.gif </ servlet-mapping >
None.gif < welcome-file-list >
None.gif < welcome-file >index.jsp </ welcome-file >
None.gif </ welcome-file-list >
None.gif </ web-app >
相關文章
相關標籤/搜索