一个联动的下拉框

多个下拉框在一起,后面的下拉框会根据前面选择的内容的变化而变化。


showCity.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>
    <base href="<%=basePath%>">
    
    <title>My JSP 'showCity.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">
	<script type="text/javascript">
	var xmlhttp;
	function GetXmlHttpObject()
	{
		 xmlHttp=null;
		try
		  {
		  // Firefox,Opera 8.0+,Safari
		  xmlHttp=new XMLHttpRequest();
		  }
		catch (e)
		  {
		  // Internet Explorer
		  try
		    {
		    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		    }
		  catch (e)
		    {
		    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		    }
		  }
		return xmlHttp;
	}
	function sendRequest(){
		GetXmlHttpObject();
		if(xmlHttp){
			var url = "servlet/ShowCities";
			var data="province="+$('sheng').value;
			xmlHttp.open("post",url,true);
			xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
			//指定回调函数  
			xmlHttp.onreadystatechange=deal;  
			//发送get请求,如果是get请求填入null  
			//如果是post请求,则填入实际的数据。  
			xmlHttp.send(data);  
			
		}
	}
	function deal(){
		//==4表示完成,200表示取出数据
		if(xmlHttp.readyState==4&&xmlHttp.status==200){
			var citys=xmlHttp.responseText;
			citys=eval("("+citys+")");
			
			$('city').length=0;
			var myOption1=document.createElement("option");
			myOption1.value="";
			myOption1.innerText="--城市--";
			$('city').appendChild(myOption1);
			
			for(var i=0;i<citys.length;++i){
				var city=citys[i].cityname;
				var myOption=document.createElement("option");
				myOption.value=city;
				myOption.innerText=city;
				$('city').appendChild(myOption);
			}
			
		}
	}
	function $(id){
		return document.getElementById(id);
	}
	</script>

  </head>
  
  <body>
    <select id="sheng" onchange="sendRequest();">
	    <option value="">---省---</option>
	    <option value="zhejiang">浙江</option>
	    <option value="jiangsu" >江苏</option>
    </select>
    <select id="city">
	    <option value="">--城市--</option>
    </select>
  </body>
</html>
showCities.java
public class ShowCities extends HttpServlet {


	public void doGet(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException {

		response.addheader("Content-Type","text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String province=request.getParameter("province");
		String res="";
		if(province.equals("zhejiang")){
			res="[{'cityname':'杭州'},{'cityname':'温州'},{'cityname':'宁波'}]";
		}else if(province.equals("jiangsu")){
			res="[{'cityname':'南京'},{'cityname':'徐州'},{'cityname':'苏州'}]";
		}
		out.println(res);
		out.flush();
		out.close();
	}


	public void doPost(HttpServletRequest request,IOException {
		doGet(request,response);
	}

}

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...