java完成的二級聯動菜單後果。本站提示廣大學習愛好者:(java完成的二級聯動菜單後果)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成的二級聯動菜單後果正文
本文實例講述了java完成的二級聯動菜單後果。分享給年夜家供年夜家參考,詳細以下:
JSP代碼:
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>二級菜單聯動演示</title> <script type="text/javascript"> var req; window.onload=function() {//頁面加載時的函數 } function Change_Select(){//當第一個下拉框的選項產生轉變時挪用該函數 var province = document.getElementById('province').value; var url = "select?id="+ escape(province); if(window.XMLHttpRequest){ req = new XMLHttpRequest(); }else if(window.ActiveXObject){ req = new ActiveXObject("Microsoft.XMLHTTP"); } if(req){ req.open("GET",url,true); //指定回調函數為callback req.onreadystatechange = callback; req.send(null); } } //回調函數 function callback(){ if(req.readyState ==4){ if(req.status ==200){ parseMessage();//解析XML文檔 }else{ alert("不克不及獲得描寫信息:" + req.statusText); } } } //解析前往xml的辦法 function parseMessage(){ var xmlDoc = req.responseXML.documentElement;//取得前往的XML文檔 var xSel = xmlDoc.getElementsByTagName('select'); //取得XML文檔中的一切<select>標志 var select_root = document.getElementById('city'); //取得網頁中的第二個下拉框 select_root.options.length=0; //每次取得新的數據的時刻先把每二個下拉框架的長度清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); //依據每組value和text標志的值創立一個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"> 二級聯動示例 </td> </tr> <tr> <td> <select name="province" id="province" onChange="Change_Select()"> <!–第一個下拉菜單–> <option value="0"> 請選擇 </option> <option value="1"> 北京 </option> <option value="2"> 天津 </option> <option value="3"> 山東 </option> </select> <select name="city" id="city"> <!–第二個下拉菜單–> <option value="0"> 請選擇 </option> </select> </td> </tr> <tr> <td> </td> <tr> </table> </form> </div> </body> </html>
Java代碼:
package com; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /*** * * @author zdw * */ public class SelectServlet extends HttpServlet { private static final long serialVersionUID = 1L; public SelectServlet() { super(); } public void destroy() { super.destroy(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // response.setCharacterEncoding("GBK"); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); request.setCharacterEncoding("GBK"); response.setCharacterEncoding("UTF-8"); String targetId = request.getParameter("id").toString(); System.out.println(targetId); // 取得要求中參數為id的值 String xml_start = "<selects>"; String xml_end = "</selects>"; String xml = ""; if (targetId.equalsIgnoreCase("0")) { xml = "<select><value>0</value><text>請選擇</text></select>"; } else if (targetId.equalsIgnoreCase("1")) { xml = "<select><value>1</value><text>昌平</text></select>"; xml += "<select><value>2</value><text>豐台</text></select>"; xml += "<select><value>3</value><text>海澱</text></select>"; xml += "<select><value>4</value><text>旭日</text></select>"; } else if (targetId.equalsIgnoreCase("2")) { xml = "<select><value>1</value><text>塘沽區</text></select>"; xml += "<select><value>2</value><text>漢沽區</text></select>"; xml += "<select><value>3</value><text>年夜港區</text></select>"; xml += "<select><value>4</value><text>東麗區</text></select>"; } else {// 假如是3,則前往上面的字符 xml = "<select><value>1</value><text>濟南</text></select>"; xml += "<select><value>2</value><text>青島</text></select>"; xml += "<select><value>3</value><text>淄博</text></select>"; xml += "<select><value>4</value><text>棗莊</text></select>"; } String last_xml = xml_start + xml + xml_end; response.getWriter().write(last_xml); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException { } }
XML代碼:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>SelectServlet</servlet-name> <servlet-class>com.SelectServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>SelectServlet</servlet-name> <url-pattern>/select</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
更多java相干內容感興致的讀者可檢查本站專題:《Java數據構造與算法教程》、《Java操作DOM節點技能總結》、《Java文件與目次操作技能匯總》和《Java緩存操作技能匯總》
願望本文所述對年夜家java法式設計有所贊助。