• Ajax案例-基于XML,以POST方式,完成省份-城市二级下拉联动


    <%@ page language="java" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>基于XML,以POST方式,完成省份-城市二级下拉联动</title>
        <script type="text/javascript" src="js/ajax.js"></script>
      </head>
      <body>
            
        <select id="provinceID" style="111px">
            <option>选择省份</option>
            <option>湖南</option>
            <option>广东</option>
        </select>    
        &nbsp;&nbsp;&nbsp;
        <select id="cityID" style="111px">
            <option>选择城市</option>
        </select>    
        
        <script type="text/javascript">
            //定位省份下拉框,同时添加内容改变事件
            document.getElementById("provinceID").onchange = function(){
                //清空城市下拉框中的内容,除第一项外
                var cityElement = document.getElementById("cityID");
                cityElement.options.length = 1;
                //获取选中option标签的索引号,从0开始            
                var index = this.selectedIndex;     
                //定位选中的option标签
                var optionElement = this[index];
                //获取选中的option标签中的内容,即省份
                var province = optionElement.innerHTML;
                //如果选中的内容不是"选择省份"
                if("选择省份" != province){
                    //NO1)
                    var ajax = createAJAX();
                    //NO2)
                    var method = "POST";
                    var url = "${pageContext.request.contextPath}/ProvinceCityServlet?time="+new Date().getTime();
                    ajax.open(method,url);
                    //设置AJAX请求头
                    ajax.setRequestHeader("content-type","application/x-www-form-urlencoded");
                    //NO3)
                    var content = "province=" + province;
                    ajax.send(content);
                    
                    //---------------------------------等待
                    
                    //NO4)
                    ajax.onreadystatechange = function(){
                        if(ajax.readyState == 4){
                            if(ajax.status == 200){
                                //NO5)从AJAX异步对象中获取服务器响应的xml文档
                                var xmlDocument = ajax.responseXML;
                                
                                //NO6)按照DOM规则,解析XML文档
                                    
                                var cityElementArray = xmlDocument.getElementsByTagName("city");    
                                var size = cityElementArray.length;
                                for(var i=0;i<size;i++){
                                    //innerHTML只能用在html/jsp中,不能用在xml中
                                    var city = cityElementArray[i].firstChild.nodeValue;
                                    //<option></option>
                                    var optionElement = document.createElement("option");    
                                    //<option>广州</option>
                                    optionElement.innerHTML = city;
                                    //<select><option>广州</option></select>
                                    cityElement.appendChild(optionElement);
                                }
                            }
                        }
                    }
                }
            }
        </script>
        
      </body>
    </html>
    package loaderman.provincecity;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * 基于XML,以POST方式,完成省份-城市二级下拉联动
     * @author AdminTC
     */
    public class ProvinceCityServlet extends HttpServlet {
        public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            String province = request.getParameter("province");
    
            //通知AJAX异步对象,服务器响应的数据为xml格式的
            response.setContentType("text/xml;charset=UTF-8");
            //获取字符输出流
            PrintWriter pw = response.getWriter();
    
            pw.write("<?xml version='1.0' encoding='UTF-8'?>");
            pw.write("<root>");
    
    
            if("广东".equals(province)){
                pw.write("<city>广州</city>");
                pw.write("<city>深圳</city>");
                pw.write("<city>中山</city>");
            }else if("湖南".equals(province)){
                pw.write("<city>长沙</city>");
                pw.write("<city>株洲</city>");
                pw.write("<city>湘潭</city>");
                pw.write("<city>岳阳</city>");
            }
    
    
            pw.write("</root>");
            pw.flush();
            pw.close();
        }
    }
  • 相关阅读:
    关于NB-IoT,没有比这篇更通俗易懂的啦!
    使用SQL Server 2014 In-Memory 内存数据库时需要注意的地方
    SQL Server Profiler:使用方法和指标说明
    SQL Server 维护计划实现数据库备份(策略实战)
    SQL Server 连接问题圣经-命名管道
    如何查看 exec sp_execute 10 XXX, XXXX的RPC事件 内容
    Mac下Qt连接MySQL 驱动问题
    {Nodejs} 错误 HPE_INVALID_CONSTANT 解决
    {Nodejs} request URL 中文乱码
    Centos 5.5 安装 Git 以及配置
  • 原文地址:https://www.cnblogs.com/loaderman/p/10044107.html
Copyright © 2020-2023  润新知