AJAX解析json之 下拉框 二级联动

转自:http://blog.csdn.net/u011955534/article/details/16114145

实现省、市二级联动,当选择某一省时,改省下面的市就会在另一个下拉框显示出来。在本例中AJAX通过解析json文件得到的数据传回到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="JsonGetP?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.         //alert(xmlHttp.responseText);  
  29.         var proText=xmlHttp.responseText;  
  30.         var pro=eval("("+proText+")");  
  31.         //alert(pro[1].province);  
  32.         var pselect=document.getElementById("sheng");  
  33.         for(var i=0;i<pro.length;i++){  
  34.             pselect.options.add(new Option(pro[i].province,pro[i].shorter));  
  35.         }  
  36.     }  
  37.   }  
  38.   function getcity(){  
  39.     xmlHttp.open("post",url,true);  
  40.     xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
  41.     var province=document.getElementById("sheng").value;  
  42.   //    alert("省:"+province);  
  43.     xmlHttp.send("province="+province);  
  44.     xmlHttp.onreadystatechange=setcity;  
  45.   }  
  46.   function setcity(){  
  47.     if(xmlHttp.readyState==4 && xmlHttp.status==200){  
  48.         var city=document.getElementById("city");  
  49.         city.options.length=0;  
  50.         var cityText=xmlHttp.responseText;  
  51.         //alert(cityText);  
  52.         var ct=eval("("+cityText+")");  
  53.         //alert(ct[1].cityname);  
  54.         for(var i=0;i<ct.length;i++){  
  55.             var cityname=ct[i].cityname;  
  56.             //alert(cityname);  
  57.             city.options.add(new Option(cityname,cityname));  
  58.         }  
  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.       
  69.     </select>  
  70.   </body>  
  71. </html>  
servlet代码:
  1. public class JsonGetP extends HttpServlet {  
  2.   
  3.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  4.             throws ServletException, IOException {  
  5.         this.doPost(request, response);  
  6.     }  
  7.   
  8.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  9.             throws ServletException, IOException {  
  10.         request.setCharacterEncoding("utf-8");  
  11.         String province = request.getParameter("province");  
  12.         if (province != null) {  
  13.             sendCity(request, response, province);  
  14.         } else {  
  15.             ShengDao sd = new ShengDao();  
  16.             List<Sheng> list = sd.selAll();  
  17.             response.setCharacterEncoding("utf-8");  
  18.             PrintWriter out = response.getWriter();  
  19.             response.setContentType("text/xml");  
  20.             out.print("[");  
  21.             for (Sheng sheng : list) {  
  22.                 JSONObject jsonobj=JSONObject.fromObject(sheng);  
  23.                 String str=jsonobj.toString();  
  24.                 out.print(str+",");  
  25.                 out.println();  
  26.             }  
  27.             out.print("]");  
  28.         }  
  29.     }  
  30.   
  31.     public void sendCity(HttpServletRequest request,  
  32.             HttpServletResponse response, String shorter) {  
  33.         try {  
  34.             request.setCharacterEncoding("utf-8");  
  35.         } catch (UnsupportedEncodingException e1) {  
  36.             e1.printStackTrace();  
  37.         }  
  38.         try {  
  39.             response.setCharacterEncoding("utf-8");  
  40.             PrintWriter out = response.getWriter();  
  41.             response.setContentType("text/html");  
  42.             ShengDao sd = new ShengDao();  
  43.             out.print("[");  
  44.             List<City> list = sd.selAll(shorter);  
  45.             for (City city : list) {  
  46.                 JSONObject jsonobj=JSONObject.fromObject(city);  
  47.                 String str=jsonobj.toString();  
  48.                 out.print(str+",");  
  49.                 out.println();  
  50.             }  
  51.             out.print("]");  
  52.         } catch (IOException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.     }  
  56.   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值