Ajax pull-down list Interaction

Source: Internet
Author: User

 

1

 

JSP page JSCode

<% @ 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; // obtain the selected value from the first drop-down menu <br/> var url = "Servlet/servlet? Id = "+ Zhi; // specify the URL for sending the request. The parameter ID is the value of the variable Zhi <br/> If (window. XMLHttpRequest) {<br/> Req = new XMLHttpRequest (); // create an XMLHTTPRequest object <br/>} else if (window. activexobject) {<br/> Req = new activexobject ("Microsoft. XMLHTTP "); <br/>}< br/> If (req) {<br/> req. open ("get", URL, true); // create a request <br/> req. onreadystatechange = callback; <br/> req. send (null); <br/>}</P> <p> function callback () {<br/> If (req. readystate = 4) {// If (req. status = 200) {// If the req status is 200, it indicates OK <br/> parsemessage (); // process the XML returned file, parse XML document </P> <p >}else {<br/> alert ("not able to retrive description" + req. statustext); <br/>}</P> <p> function parsemessage () {<br/> var xmldoc = req.responsexml.doc umentelement; // obtain the returned XML document <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/>}</P> <p> // --> </MCE: SCRIPT> <br/>

 

2 body Page code

Page body code: </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/> <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"> select </option> <br/> </SELECT> </TD> <br/> </tr> <br /> <tr> <TD> </tr> <br/> </table> <br/> </form> </P> <p> </div> </P> <p> </body> <br/>

3servlet code

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/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.