二級聯(lián)動菜單代碼

          二級聯(lián)動菜單代碼(AJAX+JAVA)

          做項目經(jīng)常要用到的,現(xiàn)在只給出一個不用數(shù)據(jù)庫的代碼,可作參考:
          /**
          *category.jsp 含有AJAX程序
          *
          */
          [java]
          <%@ page language="java" pageEncoding="UTF-8"%>
          <html>
            <head>
              <title>二級菜單聯(lián)動演示</title>
            <script type="text/javascript">
              var req;
              window.onload=function(){//頁面加載時的函數(shù)

              }
             
              function Change_Select(){//當?shù)谝粋€下拉框的選項發(fā)生改變時調(diào)用該函數(shù)
                var zhi = document.getElementById('hero').value;
                var url = "servlet/select?id="+ escape(zhi);
                if(window.XMLHttpRequest){
                  req = new XMLHttpRequest();
                }else if(window.ActiveXObject){
                  req = new ActiveXObject("Microsoft.XMLHTTP");
                }
                
                if(req){
                  req.open("GET",url,true);
                  req.onreadystatechange = callback; //指定回調(diào)函數(shù)為callback
                  req.send(null);
                }
              }
             
              function callback(){
                if(req.readyState ==4){
                  if(req.status ==200){
                    parseMessage();//解析XML文檔
                  }else{
                    alert("Not able to retrieve description" + req.statusText);
                  }
                }
              }
             
              function parseMessage(){
                var xmlDoc = req.responseXML.documentElement;//獲得返回的XML文檔
                var xSel = xmlDoc.getElementsByTagName('select');
                //獲得XML文檔中的所有<select>標記
                var select_root = document.getElementById('skill');
                //獲得網(wǎng)頁中的第二個下拉框
                select_root.options.length=0;
                //每次獲得新的數(shù)據(jù)的時候先把每二個下拉框架的長度清0
                
                for(var i=0;i<xSel.length;i++){
                  var xValue = xSel[i].childNodes[0].firstChild.nodeValue;
                  //獲得每個<select>標記中的第一個標記的值,也就是<value>標記的值
                  var xText = xSel[i].childNodes[1].firstChild.nodeValue;
                  //獲得每個<select>標記中的第二個標記的值,也就是<text>標記的值
                  
                  var option = new Option(xText, xValue);
                  //根據(jù)每組value和text標記的值創(chuàng)建一個option對象
                  
                  try{
                    select_root.add(option);//將option對象添加到第二個下拉框中
                  }catch(e){
                  }
                }
              }        
            </script>
            </head>

            <body>
            <div align="center">
            <form name="form1" method="post" action="">
            <table width="70%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td align="center">Double Select Box</td>
              </tr>
              <tr>
              <td>
              <select name="hero" id="hero" onChange="Change_Select()">
              <!--第一個下拉菜單-->
                <option value="0">Unbounded</option>
                <option value="1">D.K. </option>
                <option value="2">NEC. </option>
                <option value="3">BOSS </option>
              </select>
              <select name="skill" id="skill">
              <!--第二個下拉菜單-->
                <option value="0">Unbounded</option>
              </select>
              </td>
              </tr>
              <tr>
              <td> </td>
              <tr>
              </table>
            </form>
            </div>
            </body>
          </html>
          [/java]

          2007-3-27 15:00 bufegar
          /**
          *SelectServelet.java 處理頁面返回的數(shù)據(jù),返回XML給AJAX使用
          *
          */

          [java]
          package onlinetest.servlet;

          import java.io.IOException;

          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          public class SelectServlet extends HttpServlet {

            /**
             * The doGet method of the servlet. <br>
             *
             * This method is called when a form has its tag value method equals to get.
             *
             * @param request
             *            the request send by the client to the server
             * @param response
             *            the response send by the server to the client
             * @throws ServletException
             *             if an error occurred
             * @throws IOException
             *             if an error occurred
             */
            public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
              response.setContentType("text/xml");
              response.setHeader("Cache-Control", "no-cache");
              String targetId = request.getParameter("id").toString();
              // 獲得請求中參數(shù)為id的值
              String xml_start = "<selects>";
              String xml_end = "</selects>";
              String xml = "";

              if (targetId.equalsIgnoreCase("0")) {
                xml = "<select><value>0</value><text>Unbounded</text></select>";
              } else if (targetId.equalsIgnoreCase("1")) {
                xml = "<select><value>1</value><text>Mana Burn</text></select>";
                xml += "<select><value>2</value><text>Death Coil</text></select>";
                xml += "<select><value>3</value><text>Unhkly Aura</text></select>";
                xml += "<select><value>4</value><text>Unholy Fire</text></select>";
              } else if (targetId.equalsIgnoreCase("2")) {
                xml = "<select><value>1</value><text>Corpexplode</text></select>";
                xml += "<select><value>2</value><text>Raise Dead</text></select>";
                xml += "<select><value>3</value><text>Brilliance Aura</text></select>";
                xml += "<select><value>4</value><text>Aim Aura</text></select>";
              } else {// 如果是3,則返回下面的字符
                xml = "<select><value>1</value><text>Rain of Chaos</text></select>";
                xml += "<select><value>2</value><text>Finger of Death</text></select>";
                xml += "<select><value>3</value><text>Bash</text></select>";
                xml += "<select><value>4</value><text>Summon Doom</text></select>";
              }

              String last_xml = xml_start + xml + xml_end;
              response.getWriter().write(last_xml);

            }

            /**
             * The doPost method of the servlet. <br>
             *
             * This method is called when a form has its tag value method equals to
             * post.
             *
             * @param request
             *            the request send by the client to the server
             * @param response
             *            the response send by the server to the client
             * @throws ServletException
             *             if an error occurred
             * @throws IOException
             *             if an error occurred
             */
            public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {

              doGet(request, response);// 對于post的請求方式和get請求方式一樣處理
            }

          }
          [/java]

          2007-3-27 15:02 bufegar
          /**
          *web.xml 配置Servlet,可以使用MyEclipse創(chuàng)建Servlet,自動生成web.xml
          *
          */

          [xml]
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns="<a target="_blank">[url]http://java.sun.com/xml/ns/j2ee[/url]"</a> xmlns:xsi="<a target="_blank">[url]http://www.w3.org/2001/XMLSchema-instance[/url]"</a> version="2.4" xsi:schemaLocation="<a target="_blank">[url]http://java.sun.com/xml/ns/j2ee[/url]</a>   <a target="_blank">[url]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]"></a>
            <servlet>
              <servlet-name>select</servlet-name>
              <servlet-class>onlinetest.servlet.SelectServlet</servlet-class>
            </servlet>

            <servlet-mapping>
              <servlet-name>select</servlet-name>
              <url-pattern>/servlet/select</url-pattern>
            </servlet-mapping>
            
            <filter>
                  <filter-name>characterFilter</filter-name>
                  <filter-class>filters.SetCharacterEncodingFilter</filter-class>
                  <init-param>
                      <param-name>encoding</param-name>
                      <param-value>utf-8</param-value>
                  </init-param>
              </filter>
             <filter-mapping>
              <filter-name>characterFilter</filter-name>
              <url-pattern>/*</url-pattern>
             </filter-mapping>
          </web-app>[/xml]

          原文地址:[url]http://my.boolean.cn/read.php?130&part=3[/url]

          posted on 2007-08-14 14:32 劉錚 閱讀(1018) 評論(0)  編輯  收藏 所屬分類: Ajax

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計

          留言簿(1)

          文章分類(141)

          文章檔案(147)

          搜索

          最新評論

          主站蜘蛛池模板: 冕宁县| 扶风县| 阜南县| 虎林市| 平阴县| 财经| 喀喇沁旗| 安吉县| 肥城市| 枣庄市| 勃利县| 伊金霍洛旗| 肇源县| 当雄县| 闽侯县| 福海县| 高安市| 屯留县| 郴州市| 宁化县| 洛南县| 固阳县| 封丘县| 牟定县| 南和县| 滁州市| 广昌县| 吴川市| 基隆市| 乌什县| 翼城县| 盐山县| 西盟| 马关县| 通江县| 永定县| 玉环县| 吐鲁番市| 岚皋县| 茌平县| 临城县|