Ajax实现二级联动下拉框

这个一个ajax的经典示例,也是ajax的长处所在。不多说了,下面来看代码。

项目结构图:
option1.jpg

index.jsp:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <% @pagelanguage = " java " contentType = " text/html;charset=utf-8 " %>

< html >
< head >
< title > MyJSP ' index.jsp ' startingpage </ title >
< metahttp - equiv = " Content-Type " content = " text/html;charset=utf-8 " >
< SCRIPTtype = " text/javascript " >
varreq;
window.onload
= function(){
}

functionChange_Select()
{
varzhi
= document.getElementById( ' hero ' ).value;
varurl
= " select?id= " + escape(zhi);
if (window.XMLHttpRequest)
{
req
= new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
req
= new ActiveXObject( " Microsoft.XMLHTTP " );
}

if (req)
{
req.open(
" GET " ,url, true );
req.onreadystatechange
= callback;
req.send(
null );
}
}

functioncallback()
{
if (req.readyState == 4 )
{
if (req.status == 200 )
{
parseMessage();
}
else {
alert(
" Notabletoretrievedescription " + req.statusText);
}
}
}

functionparseMessage()
{
varxmlDoc
= req.responseXML.documentElement;
varxSel
= xmlDoc.getElementsByTagName( ' select ' );
varselect_root
= document.getElementById( ' skill ' );
select_root.options.length
= 0 ;

for (vari = 0 ;i < xSel.length;i ++ )
{
varxValue
= xSel[i].childNodes[ 0 ].firstChild.nodeValue;
varxText
= xSel[i].childNodes[ 1 ].firstChild.nodeValue;
varoption
= new Option(xText,xValue);
try {
select_root.add(option);
}
catch (e){
}
}


}
</ SCRIPT >
</ head >

< body >
< divalign = " center " >
< formname = " form1 " method = " post " action = "" >
< TABLEwidth = " 70% " boder = " 0 " cellspacing = " 0 " >
< TR >
< TDalign = " center " > DoubleSelectBox </ TD >
</ TR >
< TR >
< TD >
< SELECTname = " hero " id = " hero " onChange = " Change_Select() " >
< OPTIONvalue = " 0 " > Unbounded </ OPTION >
< OPTIONvalue = " 1 " > D.K. </ OPTION >
< OPTIONvalue = " 2 " > NEC. </ OPTION >
< OPTIONvalue = " 3 " > BOSS </ OPTION >
</ SELECT >
< SELECTname = " skill " id = " skill " >
< OPTIONvalue = " 0 " > Unbounded </ OPTION >
</ SELECT >
</ TD >
</ TR >
< TR >< td >& nbsp; </ td ></ TR >
</ TABLE >
</ form >
</ div >
</ body >
</ html >

SelectServlet.java:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> package com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SelectServlet extends HttpServlet{

/**
*Constructoroftheobject.
*/
public SelectServlet(){
super ();
}

/**
*Destructionoftheservlet.<br>
*/
public void destroy(){
super .destroy(); // Justputs"destroy"stringinlog
// Putyourcodehere
}

/**
*ThedoGetmethodoftheservlet.<br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*
@param requesttherequestsendbytheclienttotheserver
*
@param responsetheresponsesendbytheservertotheclient
*
@throws ServletExceptionifanerroroccurred
*
@throws IOExceptionifanerroroccurred
*/
public void doGet(HttpServletRequestrequest,HttpServletResponseresponse)
throws ServletException,IOException{
response.setContentType(
" text/xml " );
response.setHeader(
" Cache-Control " , " no-cache " );

StringtargetId
= request.getParameter( " id " ).toString();
Stringxml_start
= " <selects> " ;
Stringxml_end
= " </selects> " ;
Stringxml
= "" ;
if (targetId.equalsIgnoreCase( " 0 " )){
xml
= " <select><value>0</value><text>Unbounded</text></select> " ;
}
else if (targetId.equalsIgnoreCase( " 1 " )){
xml
= " <select><value>1</value><text>ManaBurn</text></select> " ;
xml
+= " <select><value>2</value><text>DeathCoil</text></select> " ;
xml
+= " <select><value>3</value><text>UnholyAura</text></select> " ;
xml
+= " <select><value>4</value><text>UnholyFire</text></select> " ;
}
else if (targetId.equalsIgnoreCase( " 2 " )){
xml
= " <select><value>1</value><text>Corprxplode</text></select> " ;
xml
+= " <select><value>2</value><text>RaiseDead</text></select> " ;
xml
+= " <select><value>3</value><text>BrillianceAura</text></select> " ;
xml
+= " <select><value>4</value><text>AimAura</text></select> " ;
}
else {
xml
= " <select><value>1</value><text>RainofChaos</text></select> " ;
xml
+= " <select><value>2</value><text>FingerofDeath</text></select> " ;
xml
+= " <select><value>3</value><text>Bash</text></select> " ;
xml
+= " <select><value>4</value><text>SummonDoom</text></select> " ;
}
Stringlast_xml
= xml_start + xml + xml_end;
response.getWriter().write(last_xml);
}

/**
*ThedoPostmethodoftheservlet.<br>
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
*
*
@param requesttherequestsendbytheclienttotheserver
*
@param responsetheresponsesendbytheservertotheclient
*
@throws ServletExceptionifanerroroccurred
*
@throws IOExceptionifanerroroccurred
*/
public void doPost(HttpServletRequestrequest,HttpServletResponseresponse)
throws ServletException,IOException{

doGet(request,response);
}

/**
*Initializationoftheservlet.<br>
*
*
@throws ServletExceptionifanerroroccure
*/
public void init() throws ServletException{
// Putyourcodehere
}

}

web.xml:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> <? xmlversion = " 1.0 " encoding = " UTF-8 " ?>
< web - appversion = " 2.4 "
xmlns
= " http://java.sun.com/xml/ns/j2ee "
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">
< servlet >
< servlet - name > SelectServlet </ servlet - name >
< servlet - class > com.SelectServlet </ servlet - class >
</ servlet >

< servlet - mapping >
< servlet - name > SelectServlet </ servlet - name >
< url - pattern >/ select </ url - pattern >
</ servlet - mapping >

< welcome - file - list >
< welcome - file > index.jsp </ welcome - file >
</ welcome - file - list >
</ web - app >

运行结果图:
option2.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值