1. 一個單獨的測試界面:javascript
- <%@ 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>
- <base href="<%=basePath%>">
-
- <title>My JSP 'new.jsp' starting page</title>
-
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css">
- -->
-
- <mce:script type="text/javascript" src="resources/jquery.js" mce_src="resources/jquery.js"></mce:script>
-
- </head>
- <body>
- <select id="Select1" name="Select1" onchange="selectProvince();"></select>
- <select id="Select2" name="Select2" onchange="selectArea();"></select>
- <select id="Select3" name="Select3"></select>
-
- </body>
- </html>
-
- <mce:script type="text/javascript"><!--
-
- window.onload = function() {
- initArea();
- }
-
- function initArea() {
- /*獲取,引用三個下拉框 */
- var dropElement1=document.getElementById("Select1");
- var dropElement2=document.getElementById("Select2");
- var dropElement3=document.getElementById("Select3");
-
- /*自定義一個方法 把傳進來的對象清除 這裏清除了三個下拉全部框的選項*/
- removeDropDownList(dropElement1);
- removeDropDownList(dropElement2);
- removeDropDownList(dropElement3);
-
- var pOption = document.createElement("option");
- pOption.value = "0";
- pOption.text = "全國";
- dropElement1.add(pOption);
-
- var cOption = document.createElement("option");
- cOption.value = "0";
- cOption.text = "所有市";
- dropElement2.add(cOption);
-
- var aOption = document.createElement("option");
- aOption.value = "0";
- aOption.text = "全區縣";
- dropElement3.add(aOption);
-
- var url = "servlet/XmlForAJAX";
- url = encodeURI(encodeURI(url));
- doAjax(url,null,null,dropElement1,dropElement2,dropElement3);
- }
-
-
- /**
- * 當選擇一個省的時候自動的添加該省下的市
- */
- function selectProvince() {
- var dropElement1=document.getElementById("Select1");
- //獲取選中項的內容
- var provinceName=dropElement1.options[dropElement1.selectedIndex].text;
-
- var dropElement2=document.getElementById("Select2");
- var dropElement3=document.getElementById("Select3");
-
- removeDropDownList(dropElement2);
- removeDropDownList(dropElement3);
-
- var cOption = document.createElement("option");
- cOption.value = "0";
- cOption.text = "所有市";
- dropElement2.add(cOption);
-
- var aOption = document.createElement("option");
- aOption.value = "0";
- aOption.text = "全區縣";
- dropElement3.add(aOption);
-
- var url = "servlet/XmlForAJAX";
- url = encodeURI(encodeURI(url));
- doAjax(url,provinceName,null,dropElement1,dropElement2,dropElement3);
- }
-
- /**
- * 當選擇一個省的時候自動的添加該省下的市
- */
- function selectArea() {
- var dropElement1=document.getElementById("Select1");
- var provinceName=dropElement1.options[dropElement1.selectedIndex].text;
- var dropElement2=document.getElementById("Select2");
- var cityName=dropElement2.options[dropElement2.selectedIndex].text;
-
- var dropElement3=document.getElementById("Select3");
- removeDropDownList(dropElement3);
- var aOption = document.createElement("option");
- aOption.value = "0";
- aOption.text = "全區縣";
- dropElement3.add(aOption);
-
- var url = "servlet/XmlForAJAX";
- url = encodeURI(encodeURI(url));
- doAjax(url,provinceName,cityName,dropElement1,dropElement2,dropElement3);
- }
-
-
- //AJAX操做類
- function doAjax(url,provinceName,cityName,dropElement1,dropElement2,dropElement3) {
- $.ajax({
- type:"POST",
- secureuri:false,
- dataType:"xml",
- url: url,
- timeout:1000,
- success:function(xml) {
-
- $(xml).find("province").each(function(i){
- var province = $(this).attr("name");
- //alert(province);
- if(provinceName == null || provinceName == "") {
- var pOption = document.createElement("option");
- pOption.value = province;
- pOption.text = province;
- dropElement1.add(pOption);
- }
-
- if(provinceName != null && provinceName != "") {
- if(provinceName == province) {
- $(this).find("city").each(function(j){
- var city = $(this).attr("name");
- //alert("city is " + city);
- if(cityName == null || cityName == "") {
- var pOption = document.createElement("option");
- pOption.value = city;
- pOption.text = city;
- dropElement2.add(pOption);
- }
-
- if(cityName != null && cityName != null) {
- if(cityName == city) {
- $(this).find("area").each(function(k){
- var area = $(this).attr("name");
- //alert("area is " + area);
- var pOption = document.createElement("option");
- pOption.value = area;
- pOption.text = area;
- dropElement3.add(pOption);
- });
- }
- }
- });
- }
- }
- });
- },
- error:function(xml) {
- alert("error.");
- }
- });
- }
-
-
- /* 清除下拉框的選項 */
- function removeDropDownList(obj) {
- //若是obj不爲空的時候
- if(obj) {
- var length = obj.options.length;
- if(length > 0) {
- for(var i=length;i >= 0;i--) {
- obj.remove(i);
- }
- }
- }
- }
-
- // --></mce:script>
2. servletcss
3. XML文件,能夠按照如下格式多添加幾個市html
- <?xml version="1.0" encoding="UTF-8"?>
-
- <root name="中國">
- <province name="北京" postcode="beijing" index="1" id="1">
- <city name="北京" postcode="100000" index="1">
- <area name="北京市" postcode="100000" price="1" index="1"/>
- <area name="門頭溝區" postcode="102300" index="2"/>
- <area name="房山區" postcode="102400" index="3"/>
- <area name="順義區" postcode="101300" index="4"/>
- <area name="通州區" postcode="101100" index="5"/>
- <area name="昌平區" postcode="102200" index="6"/>
- <area name="密雲縣" postcode="101500" index="7"/>
- <area name="延慶縣" postcode="102100" index="8"/>
- <area name="大興縣" postcode="102600" index="9"/>
- <area name="懷柔縣" postcode="101400" index="10"/>
- <area name="平谷縣" postcode="101200" index="11"/>
- </city>
- </province>
- </root>