這個(gè)例子是實(shí)現(xiàn)省、市二級(jí)聯(lián)動(dòng),當(dāng)選擇某一省時(shí),改省下面的市就會(huì)在另一個(gè)下拉框顯示出來(lái)。在本例中AJAX通過(guò)解析XML文件得到的數(shù)據(jù)傳回到j(luò)sp頁(yè)面,其中省市均是從數(shù)據(jù)庫(kù)取到的值:
jsp頁(yè)面代碼:
復(fù)制代碼 代碼如下:
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
html>
head>
/head>
script type="text/javascript">
var xmlHttp=null;
//創(chuàng)建xmlhttprequest對(duì)象
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveObject("Microsoft.XMLHTTP");
}
var url="GetProvince?time="+new Date().getTime();
function getsheng(){
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send();
xmlHttp.onreadystatechange=getprovince;
}
function getprovince(){
if(xmlHttp.readyState==4 xmlHttp.status==200){
var xmlFile=xmlHttp.responseXML;
//獲取省的節(jié)點(diǎn)
var province=xmlFile.getElementsByTagName("province");;
//獲取select標(biāo)簽
var pselect=document.getElementById("sheng");
//循環(huán)取出xml文件省信息
for(var i=0;iprovince.length;i++){
var shorter=province[i].getAttribute("name");
var provincename=province[i].text;
//循環(huán)將省信息放入select中
pselect.options.add(new Option(provincename,shorter));//(text,value)
}
}
}
function getcity(){
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var province=document.getElementById("sheng").value;
alert("省:"+province);
xmlHttp.send("province="+province);
xmlHttp.onreadystatechange=setcity;
}
function setcity(){
if(xmlHttp.readyState==4 xmlHttp.status==200){
var city=document.getElementById("city");
var cityXml=xmlHttp.responseXML;
city.options.length=0;
var citys=cityXml.getElementsByTagName("city");
for(var i=0;icitys.length;i++){
var cityname=citys[i].text;
alert(cityname);
city.options.add(new Option(cityname,cityname));
}
}
}
/script>
body onload="getsheng()">
?。簊elect name="sheng" id="sheng" onchange="getcity()">
option>請(qǐng)選擇/option>
/select>
市:select name="city" id="city">
/select>
/body>
/html>
servlet代碼:
復(fù)制代碼 代碼如下:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String province=request.getParameter("province");
if(province!=null){
sendCity(request,response,province);
}else{
ShengDao sd=new ShengDao();
ListSheng> list=sd.selAll();
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
response.setContentType("text/xml");
out.println("?xml version='1.0' encoding='UTF-8'?>");
out.println("china>");
for (Sheng sheng : list) {
out.print("province name='"+sheng.getShorter()+"'>"+sheng.getProvince()+"/province>");
out.println();
}
out.println("/china>");
}
}
public void sendCity(HttpServletRequest request, HttpServletResponse response,String shorter){
try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
try {
response.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
response.setContentType("text/xml");
ShengDao sd=new ShengDao();
ListCity> list=sd.selAll(shorter);
out.println("?xml version='1.0' encoding='UTF-8'?>");
out.println("province>");
for (City city : list) {
out.println("city name='"+city.getShorter()+"'>"+city.getCityname()+"/city>");
System.out.println("city name='"+city.getShorter()+"'>"+city.getCityname()+"/city>");
}
out.println("/province>");
} catch (IOException e) {
e.printStackTrace();
}
}
您可能感興趣的文章:- ajax三級(jí)聯(lián)動(dòng)下拉菜單效果
- AJAX省市區(qū)三級(jí)聯(lián)動(dòng)下拉菜單(java版)
- 基于Ajax實(shí)現(xiàn)下拉框聯(lián)動(dòng)顯示數(shù)據(jù)
- ThinkPHP使用心得分享-ThinkPHP + Ajax 實(shí)現(xiàn)2級(jí)聯(lián)動(dòng)下拉菜單
- JavaScript Ajax Json實(shí)現(xiàn)上下級(jí)下拉框聯(lián)動(dòng)效果實(shí)例代碼
- jquery ajax實(shí)現(xiàn)下拉框三級(jí)無(wú)刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)
- ajax讀取數(shù)據(jù)庫(kù)內(nèi)容實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)下拉選擇菜單示例
- Ajax實(shí)現(xiàn)無(wú)刷新三聯(lián)動(dòng)下拉框
- Jquery+ajax+JAVA(servlet)實(shí)現(xiàn)下拉菜單異步取值
- Ajax+Servlet實(shí)現(xiàn)無(wú)刷新下拉聯(lián)動(dòng)效果