AJAX解析XML实例之下拉框二级联动

前端之家收集整理的这篇文章主要介绍了AJAX解析XML实例之下拉框二级联动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这个例子是实现省、市二级联动,当选择某一省时,改省下面的市就会在另一个下拉框显示出来。在本例中AJAX通过解析XML文件得到的数据传回到jsp页面,其中省市均是从数据库取到的值:

jsp页面代码

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6.  
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  8. <html>
  9. <head>
  10. </head>
  11. <script type="text/javascript">
  12. var xmlHttp=null;
  13. //创建xmlhttprequest对象
  14. if(window.XMLHttpRequest){
  15. xmlHttp=new XMLHttpRequest();
  16. }else{
  17. xmlHttp=new ActiveObject("Microsoft.XMLHTTP");
  18. }
  19. var url="GetProvince?time="+new Date().getTime();
  20. function getsheng(){
  21. xmlHttp.open("post",url,true);
  22. xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  23. xmlHttp.send();
  24. xmlHttp.onreadystatechange=getprovince;
  25. }
  26. function getprovince(){
  27. if(xmlHttp.readyState==4 && xmlHttp.status==200){
  28. var xmlFile=xmlHttp.responseXML;
  29. //获取省的节点
  30. var province=xmlFile.getElementsByTagName("province");;
  31. //获取select标签
  32. var pselect=document.getElementById("sheng");
  33. //循环取出xml文件省信息
  34. for(var i=0;i<province.length;i++){
  35. var shorter=province[i].getAttribute("name");
  36. var provincename=province[i].text;
  37. //循环将省信息放入select中
  38. pselect.options.add(new Option(provincename,shorter));//(text,value)
  39. }
  40. }
  41. }
  42. function getcity(){
  43. xmlHttp.open("post","application/x-www-form-urlencoded");
  44. var province=document.getElementById("sheng").value;
  45. alert("省:"+province);
  46. xmlHttp.send("province="+province);
  47. xmlHttp.onreadystatechange=setcity;
  48. }
  49. function setcity(){
  50. if(xmlHttp.readyState==4 && xmlHttp.status==200){
  51. var city=document.getElementById("city");
  52. var cityXml=xmlHttp.responseXML;
  53. city.options.length=0;
  54. var citys=cityXml.getElementsByTagName("city");
  55. for(var i=0;i<citys.length;i++){
  56. var cityname=citys[i].text;
  57. alert(cityname);
  58. city.options.add(new Option(cityname,cityname));
  59. }
  60. }
  61. }
  62. </script>
  63. <body onload="getsheng()">
  64. 省:<select name="sheng" id="sheng" onchange="getcity()">
  65. <option>请选择</option>
  66. </select>
  67. 市:<select name="city" id="city">
  68. </select>
  69. </body>
  70. </html>
  71. @H_502_9@
  72. servlet代码
  73. public void doPost(HttpServletRequest request,HttpServletResponse response)
  74. 			throws ServletException,IOException {
  75. 		request.setCharacterEncoding("utf-8");
  76. 		String province=request.getParameter("province");
  77. 		if(province!=null){
  78. 			sendCity(request,response,province);
  79. 		}else{
  80. 		ShengDao sd=new ShengDao();
  81. 		List<Sheng> list=sd.selAll();
  82. 		response.setCharacterEncoding("utf-8");
  83. 		PrintWriter out=response.getWriter();
  84. 		response.setContentType("text/xml");
  85. 		out.println("<?xml version='1.0' encoding='UTF-8'?>");
  86. 		out.println("<china>");
  87. 		for (Sheng sheng : list) {
  88. 			out.print("<province name='"+sheng.getShorter()+"'>"+sheng.getProvince()+"</province>");
  89. 			out.println();
  90. 		}
  91. 		out.println("</china>");
  92. 	}
  93. 	}
  94. 	
  95. 	public void sendCity(HttpServletRequest request,HttpServletResponse response,String shorter){
  96. 		try {
  97. 			request.setCharacterEncoding("utf-8");
  98. 		} catch (UnsupportedEncodingException e1) {
  99. 			e1.printStackTrace();
  100. 		}
  101. 		try {
  102. 			response.setCharacterEncoding("utf-8");
  103. 			PrintWriter out=response.getWriter();
  104. 			response.setContentType("text/xml");
  105. 			ShengDao sd=new ShengDao();
  106. 			List<City> list=sd.selAll(shorter);
  107. 			out.println("<?xml version='1.0' encoding='UTF-8'?>");
  108. 			out.println("<province>");
  109. 			for (City city : list) {
  110. 				out.println("<city name='"+city.getShorter()+"'>"+city.getCityname()+"</city>");
  111. 				System.out.println("<city name='"+city.getShorter()+"'>"+city.getCityname()+"</city>");
  112. 			}
  113. 			out.println("</province>");
  114. 		} catch (IOException e) {
  115. 			e.printStackTrace();
  116. 		}
  117. 	}@H_502_9@

猜你在找的Ajax相关文章