AJAX解析XML實(shí)例之下拉框省、市二級聯(lián)動
這個(gè)例子是實(shí)現(xiàn)省、市二級聯(lián)動,當(dāng)選擇某一省時(shí),改省下面的市就會在另一個(gè)下拉框顯示出來。在本例中AJAX通過解析XML文件得到的數(shù)據(jù)傳回到j(luò)sp頁面,其中省市均是從數(shù)據(jù)庫取到的值:
jsp頁面代碼:
<%@ 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對象
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;i<province.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("?。?quot;+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;i<citys.length;i++){
var cityname=citys[i].text;
alert(cityname);
city.options.add(new Option(cityname,cityname));
}
}
}
</script>
<body onload="getsheng()">
?。?lt;select name="sheng" id="sheng" onchange="getcity()">
<option>請選擇</option>
</select>
市:<select name="city" id="city">
</select>
</body>
</html>
servlet代碼:
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();
List<Sheng> 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();
List<City> 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();
}
}
相關(guān)文章
使用加載圖片解決在Ajax數(shù)據(jù)加載中頁面出現(xiàn)短暫空白的問題(推薦)
在項(xiàng)目中用ajax異步獲取數(shù)據(jù)后有時(shí)會因?yàn)閿?shù)據(jù)問題或者網(wǎng)絡(luò)問題,頁面一直顯示空白,現(xiàn)在用加載圖片來過渡這種狀態(tài),具體實(shí)例代碼通過本文一起學(xué)習(xí)吧2016-12-12laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn)
這篇文章主要介紹了laravel ajax curd 搜索登錄判斷功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04JavaScript如何控制Session實(shí)現(xiàn)原理及代碼
看到這個(gè)題目,或許有人會提出疑問,JavaScript代表客戶端,而Session代表的是服務(wù)器(不知道這樣說大家是否能夠理解)現(xiàn)在就回到了題目所描述的問題寫一個(gè)JavaScript方法,使其修改Session,感興趣的朋友可以了解下,或許本文對你學(xué)習(xí)ajax有所幫助2013-02-02ajax、axios和fetch之間優(yōu)缺點(diǎn)重點(diǎn)對比總結(jié)
今天被問到用沒用過ajax axios,我回答經(jīng)常用axios,但ajax用的比較少,下面這篇文章主要給大家介紹了關(guān)于ajax、axios和fetch之間優(yōu)缺點(diǎn)重點(diǎn)對比總結(jié)的相關(guān)資料,需要的朋友可以參考下2022-12-12Servlet 與 Ajax 交互一直報(bào)status=parsererror的解決辦法
這篇文章主要介紹了Servlet 與 Ajax 交互一直報(bào)status=parsererror的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03ajax實(shí)現(xiàn)文件異步上傳并回顯文件相關(guān)信息功能示例
這篇文章主要介紹了ajax實(shí)現(xiàn)文件異步上傳并回顯文件相關(guān)信息功能,結(jié)合實(shí)例形式分析了基于jQuery $.ajax方法的文件異步上傳以及后臺java程序?qū)ξ募畔⒌淖x取與顯示相關(guān)操作技巧,需要的朋友可以參考下2018-06-06