AJAX解析XML執行個體之下拉框省、市二級聯動_AJAX相關

來源:互聯網
上載者:User
這個例子是實現省、市二級聯動,當選擇某一省時,改省下面的市就會在另一個下拉框顯示出來。在本例中AJAX通過解析XML檔案得到的資料傳回到jsp頁面,其中省市均是從資料庫取到的值:

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;
//建立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;
//擷取省的節點
var province=xmlFile.getElementsByTagName("province");;
//擷取select標籤
var pselect=document.getElementById("sheng");
//迴圈取出xml檔案省資訊
for(var i=0;i<province.length;i++){
var shorter=province[i].getAttribute("name");
var provincename=province[i].text;
//迴圈將省資訊放入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;i<citys.length;i++){
var cityname=citys[i].text;
alert(cityname);
city.options.add(new Option(cityname,cityname));
}
}
}
</script>
<body onload="getsheng()">
省:<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();
}
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.