National provincial/municipal linkage Ajax asynchronous submission

Source: Internet
Author: User

JS Code:

<SCRIPT type = "text/JavaScript" Language = "JavaScript">
Function getxmlhttp (){
VaR http_request = false;
If (window. XMLHttpRequest ){
Http_request = new XMLHttpRequest ();
If (http_request.overridemimetype ){
Http_request.overridemimetype ('text/xml ');
}
} Else if (window. activexobject ){
Try {
Http_request = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Http_request = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){
}
}
}
If (! Http_request ){
Alert ('<% = mess %> ');
Return false;
}
Return http_request;
}

Function getcity (value)
{
VaR ul = "ajaxgetcitybyproid? Proid = "+ value;
Req = getxmlhttp ();
Req. onreadystatechange = result1;
Req. Open ("get", UL, true );
Req. setRequestHeader ('If-modified-since ', '0 ');
Req. Send (null );
}

Function result1 (){
If (req. readystate = 4 & Req. Status = 200)
{
VaR citys = Req. responsetext. tostring ();
Document. getelementbyid ("divcity"). innerhtml = citys;
}
}
</SCRIPT>

 

 

Some HTML code:

<Tr bgcolor = # ad8b4d>
<TD align = right width = "97">
<Bean: Message key = "regist. Province"/>
</TD>
& Lt; TD width = "387" align = "Left" & gt;
<Select name = "Province" id = "Province" style = "width: 140px;" class = Input
Onchange = "getcity (this. Options [This. Options. selectedindex]. Value);">
<Option>
<Bean: Message key = "regist. Select"/>
</Option>
<%
Province Pr = NULL;
For (INT I = 0; I <allpros. Size (); I ++ ){
PR = (province) allpros. Get (I );
%>
<Option value = "<% = Pr. getproid () %>">
<% = Pr. getproname () %>
</Option>
<%
}
%>
</SELECT>
</TD>
</Tr>

<Tr bgcolor = # ad8b4d>
<TD align = right width = "97">
<Bean: Message key = "regist. City"/>
</TD>
& Lt; TD width = "387" align = "Left" & gt;
<Span id = "divcity"> </span>
</TD>
</Tr>

 

Some servlet code:

Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
String Path = request. getcontextpath ();
Response. setcontenttype ("text/XML; charset = GBK ");
Printwriter out = response. getwriter ();

String proids = request. getparameter ("proid ");
Long proid = long. parselong (proids );

Servletcontext application = request. getsession (). getservletcontext ();
Webapplicationcontext webcontext = webapplicationcontextutils
. Getrequiredwebapplicationcontext (application );
Userservice users = (userservice) webcontext. getbean ("userservice ");

List allcity = NULL;
Try {
Allcity = users. getallcitybyproid (proid );
City city = NULL;

Stringbuffer citystr = new stringbuffer ();
Citystr. append ("<select name =/" city/"> ");
For (INT I = 0; I <allcity. Size (); I ++ ){
City = (city) allcity. Get (I );
Citystr. append ("<option value = '" + city. getcityid () + "'> ")
. Append (city. getcityname (). append ("</option> ");
}
Citystr. append ("</SELECT> ");
Out. Print (citystr );
} Catch (exception e ){
Response. sendredirect (path + "/error. jsp ");
}
}

Related Article

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.