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 ");
}
}