Ajax實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單
本文實(shí)例為大家分享了ajax二級(jí)聯(lián)動(dòng)菜單的具體代碼,供大家參考,具體內(nèi)容如下
index.jsp
<%@ page language="java" pageencoding="utf-8"%> <html> <head> <title>二級(jí)菜單聯(lián)動(dòng)演示</title> <script type="text/javascript"> var req; window.οnlοad=function() {//頁面加載時(shí)的函數(shù) } function change_select(){//當(dāng)?shù)谝粋€(gè)下拉框的選項(xiàng)發(fā)生改變時(shí)調(diào)用該函數(shù) 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); //指定回調(diào)函數(shù)為callback req.onreadystatechange = callback; req.send(null); } } //回調(diào)函數(shù) 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>標(biāo)記 var select_root = document.getelementbyid('city'); //獲得網(wǎng)頁中的第二個(gè)下拉框 select_root.options.length=0; //每次獲得新的數(shù)據(jù)的時(shí)候先把每二個(gè)下拉框架的長(zhǎng)度清0 for(var i=0;i<xsel.length;i++){ var xvalue = xsel[i].childnodes[0].firstchild.nodevalue; //獲得每個(gè)<select>標(biāo)記中的第一個(gè)標(biāo)記的值,也就是<value>標(biāo)記的值 var xtext = xsel[i].childnodes[1].firstchild.nodevalue; //獲得每個(gè)<select>標(biāo)記中的第二個(gè)標(biāo)記的值,也就是<text>標(biāo)記的值 var option = new option(xtext, xvalue); //根據(jù)每組value和text標(biāo)記的值創(chuàng)建一個(gè)option對(duì)象 try{ select_root.add(option);//將option對(duì)象添加到第二個(gè)下拉框中 }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"> 二級(jí)聯(lián)動(dòng)示例 </td> </tr> <tr> <td> <select name="province" id="province" onchange="change_select()"> <!--第一個(gè)下拉菜單--> <option value="0"> 請(qǐng)選擇 </option> <option value="1"> 北京 </option> <option value="2"> 天津 </option> <option value="3"> 山東 </option> </select> <select name="city" id="city"> <!--第二個(gè)下拉菜單--> <option value="0"> 請(qǐng)選擇 </option> </select> </td> </tr> <tr> <td> </td> <tr> </table> </form> </div> </body> </html>
selectservlet.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); // 獲得請(qǐng)求中參數(shù)為id的值 string xml_start = "<selects>"; string xml_end = "</selects>"; string xml = ""; if (targetid.equalsignorecase("0")) { xml = "<select><value>0</value><text>請(qǐng)選擇</text></select>"; } else if (targetid.equalsignorecase("1")) { xml = "<select><value>1</value><text>昌平</text></select>"; xml += "<select><value>2</value><text>豐臺(tái)</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>塘沽區(qū)</text></select>"; xml += "<select><value>2</value><text>漢沽區(qū)</text></select>"; xml += "<select><value>3</value><text>大港區(qū)</text></select>"; xml += "<select><value>4</value><text>東麗區(qū)</text></select>"; } else {// 如果是3,則返回下面的字符 xml = "<select><value>1</value><text>濟(jì)南</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 { } }
web.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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。