Java asynchronous implementation of provincial and municipal linkage

Source: Internet
Author: User

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

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.