AJAX下拉式清單互動

來源:互聯網
上載者:User

 

1

 

  JSP 頁面的JS代碼

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><br /><%@page import="svse.zp.bean.TogAdminDao"%><br /><%@page import="svse.zp.bean.AdminInfo"%><br /><mce:script type="text/javascript"><!--<br /> var req;<br /> window.onload = function(){<br /> }<br /> function Change_Select(){<br /> var zhi = document.getElementById('team').value; //獲得第一個下拉式功能表選中的值<br /> var url = "servlet/Servlet?id=" + zhi; //指定發送請求的url,參數id是變數zhi的值<br /> if(window.XMLHttpRequest){<br /> req = new XMLHttpRequest(); //建立XMLHTTPRequest對象<br /> }else if(window.ActiveXObject){<br /> req = new ActiveXObject("Microsoft.XMLHTTP");<br /> }<br /> if(req){<br /> req.open("GET",url,true); //建立請求<br /> req.onreadystatechange = callback;<br /> req.send(null);<br /> }<br /> }</p><p> function callback(){<br /> if(req.readyState == 4){ //req的狀態為4代表已載入<br /> if(req.status == 200){ //req狀態為200代表ok<br /> parseMessage(); //處理XML返迴文件,解析XML文檔</p><p> }else{<br /> alert("Not able to retrive description" + req.statusText);<br /> }<br /> }<br /> }</p><p> function parseMessage(){<br /> var xmlDoc = req.responseXML.documentElement; //獲得返回的XML文檔<br /> var xSel = xmlDoc.getElementsByTagName('select');</p><p> var select_root = document.getElementById('player');<br /> select_root.options.length=0;</p><p> for(var i=0;i<xSel.length;i++){<br /> var xValue = xSel[i].childNodes[0].firstChild.nodeValue;<br /> var xText = xSel[i].childNodes[1].firstChild.nodeValue;<br /> var option = new Option(xText,xValue);</p><p> try<br /> {<br /> select_root.add(option);<br /> }<br /> catch(e){}<br /> }<br /> }</p><p>// --></mce:script><br />

 

2  BODY 頁面代碼

頁面body代碼:</p><p><body><br /><div align="center"><br /> <form name="form1" method="post" action=""><br /> <table width="70%" border="0" cellpadding="0"><br /> <tr><br /> <td align="center">Double select box</td><br /> </tr><br /> <tr><br /> <td> <select name="team" id="team" onChange="Change_Select()"><br /><%TogAdminDao dao=new TogAdminDao();<br />List<AdminInfo> lsit=(List<AdminInfo>)dao.getAdminInfo();<br /> for(AdminInfo li:lsit){<br />%><br /> <option value="<%=li.getAdminID() %>"><%=li.getAdminName() %></option><br /> <%} %><br /> </select><br /> <select name="player" id="player"><br /> <option value="0">請選擇</option><br /> </select></td><br /> </tr><br /> <tr><td> </td></tr><br /> </table><br /> </form></p><p></div></p><p></body><br />

3servlet代碼

response.setContentType("text/xml");<br /> response.setHeader("Cache-Control", "no-cache");<br /> response.setCharacterEncoding("utf-8");<br /> int targetId=Integer.parseInt(request.getParameter("id"));<br /> System.out.println(targetId+"");<br /> String xml_start = "<selects>";<br /> String xml_end = "</selects>";<br /> String xml="";<br /> TogAdminDao dao=new TogAdminDao();</p><p> AdminInfo b=dao.getAdmin(targetId);<br /> xml = "<select><value>"+b.getAdminPwd()+"</value><text>"+b.getAdminPwd()+"</text></select>";<br /> String last_xml = xml_start + xml + xml_end;<br /> response.getWriter().write(last_xml);<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.