zoukankan      html  css  js  c++  java
  • 利用Ajax和JSON实现关于查找省市名称的二级联动功能

      功能实现的思路:我们经常碰见网上购物时候填写收件地址会用到这个查找省市县的三级联动查找功能,我们可以利用Ajax和JSON技术模拟这个功能,说白了同样是使用Ajax的局部数据更新功能这个特性。因为省市都会有很多个,所以还需要使用JSONArray对象。当我们选择某一个省的时候,会自动触发一个局部更新功能,这个功能的作用就是把我们选择的关于这个省的所有市名全部在指定的位置显示出来,当换成另外一个省的时候,还能自动把前边的选择的别的省所包含的市名全部删除换成当前选择省包含的市名。

    实现步骤:

    1、设计前端界面(毫无艺术感的前端界面):

     1 <body>
     2     省:
     3     <select id="sheng" onchange="loadInfo()">
     4         <option value="1">河南省</option>
     5         <option value="2">四川省</option>
     6         <option value="3">浙江省</option>
     7         <option value="4">山东省</option>
     8     </select>
     9     &nbsp;
    10     &nbsp;
    11     市:
    12     <select id="shi">        
    13     </select>
    14 </body>

    2、利用XMLHttpRequset对象放松请求并且接收从服务器端传来的处理结果:

    (这里需要注意的是添加重置操作是必要的,因为添加进JSONArray数组的数据如果不清除的话,下一次输出结果的时候还会输出出来,这样会相当于把不属于这个省的市名也给输出出来了,这显然不符合设计要求)

     1 <script type="text/javascript">
     2     function loadInfo(){
     3         var shengID = document.getElementById("sheng").value;
     4         //这里要添加一个重置操作,确保每一次查询之前JsonArray数组是空的。
     5         document.getElementById("shi").options.length=0;
     6         //获取XMLHttpRequest对象(有些浏览器没有XMLHttpRequest这个对象,所以获取之前需要先判断一下)
     7         var xmlHttp;
     8         if (window.XMLHttpRequest) {
     9             xmlHttp = new XMLHttpRequest();
    10         } else {
    11             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    12         }
    13         xmlHttp.onreadystatechange = function() {
    14             if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
    15                 //alert(xmlHttp.responseText);
    16                 var dataObj = eval("(" + xmlHttp.responseText + ")");
    17                 for(var i=0;i<dataObj.rows.length;i++){
    18                     var o=dataObj.rows[i];
    19                     //利用Selectobject对象的options方法动态的添加option选项
    20                     document.getElementById("shi").options.add(new Option(o.text,o.id));
    21                 }
    22                 
    23             }
    24         }
    25         xmlHttp.open("get", "getAjaxName2?shengID="+shengID, true);
    26         xmlHttp.send();
    27     }
    28 </script>

    3、编写处理该请求的后台servlet代码:(这里依然没有使用数据库查询功能,而是手动添加了每个 省份包含的市名作为例子使用。)

     1 package com.java1234.web;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 
     6 import javax.servlet.ServletException;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 import net.sf.json.JSONArray;
    12 import net.sf.json.JSONObject;
    13 
    14 public class GetAjaxNameServlet2 extends HttpServlet {
    15 
    16     /**
    17      * 
    18      */
    19     private static final long serialVersionUID = 1L;
    20 
    21     @Override
    22     protected void doGet(HttpServletRequest request, HttpServletResponse response)
    23             throws ServletException, IOException {
    24         this.doPost(request, response);
    25     }
    26 
    27     @Override
    28     protected void doPost(HttpServletRequest request, HttpServletResponse response)
    29             throws ServletException, IOException {
    30         response.setContentType("text/html;charset=utf-8");
    31         PrintWriter out = response.getWriter();
    32         //取出从前端页面传入的待查询省份的shengID
    33         String shengID = request.getParameter("shengID");
    34         JSONObject resultJson = new JSONObject();
    35         /**
    36          * JSONArray对象的作用:每个省份都会有好多个市,当确认是那个省之后,
    37          * 就会把每个省的所有市 的名称包含ID全部添加进JSONArray对象中,最后再把这个包含
    38          * 待查询市的JSONArray对象当成一个对象添加进名为resultJson的JSONObject对象中。
    39          */
    40         JSONArray jsonArray = new JSONArray();
    41         JSONObject temp = null;
    42         //这里只是模拟一下数据库查询操作
    43         switch (Integer.parseInt(shengID)) {
    44             case 1: {
    45                 temp = new JSONObject();temp.put("id", 1);temp.put("text", "郑州市");
    46                 jsonArray.add(temp);                
    47                 temp = new JSONObject();temp.put("id", 2);temp.put("text", "南阳市");
    48                 jsonArray.add(temp);                
    49                 temp = new JSONObject();temp.put("id", 3);temp.put("text", "周口市");
    50                 jsonArray.add(temp);                
    51                 temp = new JSONObject();temp.put("id", 4);temp.put("text", "商丘市");
    52                 jsonArray.add(temp);
    53                 break;
    54             }
    55             case 2: {
    56                 temp = new JSONObject();temp.put("id", 5);temp.put("text", "成都市");
    57                 jsonArray.add(temp);                
    58                 temp = new JSONObject();temp.put("id", 6);temp.put("text", "绵阳市");
    59                 jsonArray.add(temp);                
    60                 temp = new JSONObject();temp.put("id", 7);temp.put("text", "乐山市");
    61                 jsonArray.add(temp);                
    62                 temp = new JSONObject();temp.put("id", 8);temp.put("text", "达州市");
    63                 jsonArray.add(temp);
    64                 break;
    65             }
    66             case 3: {
    67                 temp = new JSONObject();temp.put("id", 9);temp.put("text", "杭州市");
    68                 jsonArray.add(temp);                
    69                 temp = new JSONObject();temp.put("id", 10);    temp.put("text", "温州市");
    70                 jsonArray.add(temp);                
    71                 temp = new JSONObject();temp.put("id", 11);temp.put("text", "南通市");
    72                 jsonArray.add(temp);                
    73                 temp = new JSONObject();temp.put("id", 12);    temp.put("text", "宁波市");
    74                 jsonArray.add(temp);
    75                 break;
    76             }
    77             case 4: {
    78                 temp = new JSONObject();temp.put("id", 13);temp.put("text", "济南市");
    79                 jsonArray.add(temp);                
    80                 temp = new JSONObject();temp.put("id", 14);    temp.put("text", "青岛市");
    81                 jsonArray.add(temp);                
    82                 temp = new JSONObject();temp.put("id", 15);temp.put("text", "泰安市");
    83                 jsonArray.add(temp);                
    84                 temp = new JSONObject();temp.put("id", 16);    temp.put("text", "聊城市");
    85                 jsonArray.add(temp);
    86                 break;
    87             }
    88         }
    89         resultJson.put("rows", jsonArray);
    90         out.println(resultJson);
    91         out.flush();
    92         out.close();
    93     }
    94 
    95 }

    4、配置web.xml文件然后运行就能实现这个功能。(运行结果是一个截图不再贴出来了,结果不重要,过程实现好了结果自然而然的不会差!)

      总结:这是一个省市查询问题的二级联动,相对三级联动还算简单一点,但这简单的例子也是能帮助自己理解这一类问题的。这里我按自己的理解说明一下三级联动的实现思路:就是当我们选择到某一个市的时候,还要再触发一个 事件,这个事件就是要动态查找数据库然后把关于这个市所包含的所有县级城市名称全部输出到指定的位置,当然了,这个触发事件的实现函数还是要像查找市级城市名称方法一样获取选择的市级城市然后作为参数传入后台进行查询并把结果全部封装到一个JSONObject对象中,然后在前端进行数据处理并进行展示。

  • 相关阅读:
    12月14日 bs-grid , destroy_all()
    12月13日 什么是help_method,session的简单理解, find_by等finder method
    12月10日 render( locals:{...}) 传入本地变量。
    12月8日 周五 image_tag.
    12月7日,几个错误,拼写错误,遗漏符号:,记忆有误,max-width的作用。gem mini_magick, simple_form
    程序员必读之软件架构
    先进PID控制MATLAB仿真(第4版)
    中文版Illustrator CS6基础培训教程(第2版)
    Android系统级深入开发——移植与调试
    Excel在会计与财务管理中的应用
  • 原文地址:https://www.cnblogs.com/BaoZiY/p/10054206.html
Copyright © 2011-2022 走看看