1.
Province: <select id= "Provinceselect" ></select> City: <select id= "Cityselect" ></select>
2.get,post requests are available
Public String findprovinces (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {list<province> list=dao.findprovinces (); String jsonstring=jsonarray.fromobject (list). toString (); Response.getwriter (). write (jsonstring); return null;} Public String Findcitysbypid (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {String pid=request.getparameter ("pid"); int provinceid=integer.parseint (PID); List<city> list=dao.findcitysbypid (Provinceid); String jsonstring=jsonarray.fromobject (list). toString (); Response.getwriter (). write (jsonstring); return null;}
3.
function Initprovinceselect () { $.getjson ("<c:url value= ' Provincecityservlet '/>", {"Method": " Findprovinces "},function" (data) {///Another traversal mode is also traversed by each way but is not commonly used here to traverse the collection of jquery elements for (var i = 0; i < data.length ; i++) { $ ("#provinceSelect"). Append ("<option value=" "+data[i].pid+" ' > "+data[i].name+" </option> ")} Bindcityselectchange ();///Note that the order of the bound events is always placed after the dependent control is loaded, because it is important to be asynchronous loading // initialization is usually unexpected $ ("# Provinceselect "). Change (); });
4.
function Bindcityselectchange () { $ ("#provinceSelect"). Change (function () { var pid=$ (this). Val ();// getting the Select item is important $ ("#citySelect"). empty (); Post get all line $.post ("<c:url value= ' Provincecityservlet '/>", {"method": "Findcitysbypid", "PId":p ID}, function (data) {for (var key in data) { $ ("#citySelect"). Append ("<option>" +data[key].name+ " (</option> ");}} ," json ");} );
Java asynchronous implementation of provincial and municipal linkage