通过jquery的ajax+mysql+servlet实现省市二级联动菜单

http://youzhibing.iteye.com/blog/1669626

 
   前提:有jquery.ajax基础、对servlet熟悉、有数据库基础 
原理:加载页面时向数据库获取所有的省展示在select元素中,当用户选中某个省份时,向服务器发送ajax请求,获取对应省份下的城市 
1、html页面代码 
   
Html代码   收藏代码
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5. <title>二级联动菜单</title>  
  6. <script src="js/jquery-1.8.0.js" type="text/javascript"></script>  
  7. <script type="text/javascript">  
  8.     jQuery(function(){  //等待DOM加载完毕.  
  9.         var province = jQuery("#province");  
  10.         var city = jQuery("#city");  
  11.         getProvince();  
  12.         province.change(function(){  
  13.             getCity(province.val());    //参数是选择框选中的value  
  14.             //jQuery("#oppro").attr("disabled","true");  
  15.         });  
  16.         })  
  17.     });  
  18.       
  19.     function getProvince(){  
  20.         jQuery.ajax({  
  21.                 type: "post",  
  22.                 url: "data",  
  23.                 data:{param:"province"},  
  24.                 success: function(data){  
  25.                     var pros =(new Function("","return "+data))();  
  26.                     for(var i=0;i<pros.data.length;i++){  
  27.                         jQuery("#province").append("<option value="+(i+1)+">" + pros.data[i] + "</option>");  
  28.                     }  
  29.                 }  
  30.         });  
  31.     }  
  32.     function getCity(provinceValue){  
  33.         if("0" == provinceValue){  
  34.             jQuery("#city").empty();  
  35.             jQuery("#city").append("<option value='0'>请选择城市</option>");  
  36.         }else{  
  37.             jQuery.ajax({  
  38.                     type: "post",  
  39.                     url: "data",  
  40.                     data:{param:"city",optionValue:provinceValue},  
  41.                     success: function(data){  
  42.                         var citys =(new Function("","return "+data))();  
  43.                         jQuery("#city").empty();  
  44.                         jQuery("#city").append("<option value='0'>请选择城市</option>");  
  45.                         for(var i=0;i<citys.data.length;i++){  
  46.                             jQuery("#city").append("<option value="+(i+1)+">" + citys.data[i] + "</option>");  
  47.                         }  
  48.                     }  
  49.             });  
  50.         }  
  51.     }  
  52. </script>  
  53. </head>  
  54. <body>  
  55.         <form id="form1">  
  56.             <select id="province">  
  57.                 <option value="0" id="oppro">请选择省份:</option>  
  58.             </select>  
  59.             <select id="city">  
  60.                 <option value="0" id="opcity">请选择城市:</optio  
  61.             </select>   
  62.         </form>  
  63.   
  64. </body>  
  65. </html>  

   html页面主要是jquery的ajax,至于页面如何发送消息给服务器、服务器如何返回消息、页面如何获取服务器返回的消息,这些问题在我的 http://youzhibing.iteye.com/admin/blogs/1636755这篇博客中有提到,原理是一样的 
2、服务器端:servlet 
  
Java代码   收藏代码
  1. @Override  
  2.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  3.             throws ServletException, IOException {  
  4.         this.doPost(req, resp);  
  5.     }  
  6.   
  7.     @Override  
  8.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  9.             throws ServletException, IOException {  
  10.           
  11.         String param = req.getParameter("param");  
  12.         ResultSet rs = null;  
  13.           
  14.         //取得省份列表  
  15.         if("province".equals(param)){  
  16.             try {  
  17.                 rs = getPreparedStatement().executeQuery();  
  18.             } catch (SQLException e) {  
  19.                 e.printStackTrace();  
  20.             }  
  21.         }  
  22.         //取得城市列表  
  23.         if("city".equals(param)){  
  24.             int optionValue = Integer.parseInt(req.getParameter("optionValue"));  
  25.             try {  
  26.                 rs = getPreparedStatement(optionValue).executeQuery();  
  27.             } catch (SQLException e) {  
  28.                 e.printStackTrace();  
  29.             }  
  30.         }  
  31.           
  32.         //返回数据给页面  
  33.         String data="{data:[";  
  34.         try {  
  35.             while(rs.next()){  
  36.                 data += "\"" + rs.getString(2) + "\",";  
  37.             }  
  38.         } catch (SQLException e) {  
  39.             e.printStackTrace();  
  40.         }  
  41.         data = data.substring(0, data.lastIndexOf(',')) + "]}";  
  42.         resp.setContentType("text/html;charset=gb2312");     
  43.         resp.setCharacterEncoding("gb2312");// 防止弹出的信息出现乱码  
  44.         resp.getWriter().write(data);  
  45.     }  

       配置文件:web.xml 
         
Xml代码   收藏代码
  1. <welcome-file-list>  
  2.     <welcome-file>index.html</welcome-file>  
  3.   </welcome-file-list>  
  4.   <servlet>  
  5.     <servlet-name>data</servlet-name>     
  6.     <servlet-class>com.province.city.DataFromMysql</servlet-class>  
  7.   </servlet>  
  8.   <servlet-mapping>  
  9.     <servlet-name>data</servlet-name>  
  10.     <url-pattern>/data</url-pattern>  
  11.   </servlet-mapping>  

  有问题、有意见都可以给我留言哦! 
   下面提供了myeclipse下的项目的压缩文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值