js使用xml數(shù)據(jù)載體實現(xiàn)城市省份二級聯(lián)動效果
更新時間:2021年08月24日 16:30:56 作者:evan_qb
這篇文章主要為大家詳細介紹了js使用xml數(shù)據(jù)載體實現(xiàn)城市省份二級聯(lián)動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了使用xml數(shù)據(jù)載體實現(xiàn)城市省份二級聯(lián)動的具體代碼,供大家參考,具體內(nèi)容如下
首先寫好前臺頁面testProvince.jsp,將請求通過open、send發(fā)送到服務(wù)器
<%@ 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%>" rel="external nofollow" > <title>二級聯(lián)動</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"> <style type="text/css"> select{ width:111px; } </style> </head> <body> <select id="provinceID"> <option>選擇省份</option> <option>湖南</option> <option>廣東</option> </select> <select id="cityID"> <option>選擇城市</option> </select> </body> <script type="text/javascript"> //創(chuàng)建ajax對象 function createAjax(){ var ajax = null; try{ ajax = new ActiveXObject("microsoft.xmlhttp"); }catch(e){ try{ ajax = new XMLHttpRequest(); }catch(e1){ alert("請更換瀏覽器"); } } return ajax; } </script> <script type="text/javascript"> document.getElementById("provinceID").onchange = function(){ //清空城市除了第一項 var cityElem = document.getElementById("cityID"); cityElem.options.length = 1; //獲取選中的省份 var province = this.value; //進行編碼處理 province = encodeURI(province); if("選擇省份" != province){ var ajax = createAjax(); //提交方式為GET var method = "GET"; //提交路徑為servlet路徑 var url = "${pageContext.request.contextPath}/ProvinceServlet?time=" + new Date().getTime()+ "&province=" +province; //準備發(fā)送異步請求 ajax.open(method, url); //由于是get請求,所以不需要設(shè)置請求頭 //發(fā)送請求 ajax.send(null); //監(jiān)聽服務(wù)器響應(yīng)狀態(tài)的變化 ajax.onreadystatechange = function(){ //響應(yīng)狀態(tài)為4 表示ajax已經(jīng)完全接受到服務(wù)器的數(shù)據(jù)了 if(ajax.readyState == 4){ //接收到的數(shù)據(jù)正常 if(ajax.status == 200){ //獲取服務(wù)器傳來的html數(shù)據(jù) var xmlDocument = ajax.responseXML; //進行dom操作解析xml //解析xml數(shù)據(jù) var citys = xmlDocument.getElementsByTagName("city"); for(var i = 0; i< citys.length;i++){ //獲取xml中的值 :不能用innerHTML,要用nodeValue var city = citys[i].firstChild.nodeValue; //創(chuàng)建option var optElement = document.createElement("option"); optElement.innerHTML = city; //獲取city var cityElems = document.getElementById("cityID"); cityElems.appendChild(optElement); } } } } } } </script> </html>
然后在后臺ProvinceServlet中通過GET方式獲取請求,將返回的數(shù)據(jù)以O(shè)(輸出)流的方式發(fā)送出去,上面代碼的ajax.responseXML獲取輸出的數(shù)據(jù),并進行dom操作
public class ProvinceServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); String province = req.getParameter("province"); //重新編碼 province = new String(province.getBytes("ISO-8859-1"),"utf-8"); //設(shè)置格式為xml resp.setContentType("text/xml;charset=utf-8"); //獲取字符輸出流 PrintWriter pw = resp.getWriter(); //拼接xml頭 pw.write("<?xml version='1.0' encoding='UTF-8'?>"); pw.write("<citys>"); if ("湖南".equals(province)) { pw.write("<city>長沙</city>"); pw.write("<city>株洲</city>"); pw.write("<city>湘潭</city>"); pw.write("<city>岳陽</city>"); }else if("廣東".equals(province)){ pw.write("<city>廣州</city>"); pw.write("<city>深圳</city>"); pw.write("<city>中山</city>"); } pw.write("</citys>"); pw.flush(); pw.close(); } }
運行結(jié)果如下:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
javascript中強制執(zhí)行toString()具體實現(xiàn)
Javascript通常會根據(jù)方法或運算符的需要而自動把值轉(zhuǎn)成所需的類型,這可能導(dǎo)致各種錯誤,接下來為大家介紹下javascript如何強制執(zhí)行toString(),感興趣的朋友可以參考下哈2013-04-04