国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > AJAX > 正文

AJAX解析XML實例之下拉框省、市二級聯動

2024-09-01 08:32:20
字體:
來源:轉載
供稿:網友
實現省、市二級聯動,當選擇某一省時,改省下面的市就會在另一個下拉框顯示出來,下面有個不錯的示例,需要的朋友可以參考下

這個例子是實現省、市二級聯動,當選擇某一省時,改省下面的市就會在另一個下拉框顯示出來。在本例中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>
省:<select onchange="getcity()">
<option>請選擇</option>
</select>
市:<select>

</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>"+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>"+city.getCityname()+"</city>");
System.out.println("<city>"+city.getCityname()+"</city>");
}
out.println("</province>");
} catch (IOException e) {
e.printStackTrace();
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 买车| 平塘县| 弥渡县| 菏泽市| 南通市| 集贤县| 临江市| 阿拉善盟| 滕州市| 长海县| 东平县| 大足县| 富平县| 东乡| 乌拉特前旗| 仙居县| 岳阳县| 平江县| 洪洞县| 云南省| 朝阳县| 石阡县| 旬阳县| 夏津县| 兰西县| 潼南县| 故城县| 茂名市| 皮山县| 高淳县| 金堂县| 昌平区| 邓州市| 华蓥市| 西乌珠穆沁旗| 柳江县| 崇礼县| 辽宁省| 斗六市| 太和县| 清水县|