Three-Level Linkage

Source: Internet
Author: User

:



1. List. jsp

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "pageencoding =" UTF-8 "%> <HTML> 

Ii. List. js

$ (Function () {$ provinceselect = $ ("# Province"); $ cityselect = $ ("# City"); $ countyselect = $ ("# county "); // display Level 1 $. post (".. /provinceservlet ", function (jsonarray) {showprovinces (jsonarray) ;}," JSON "); // display Level 2, add a response event $ provinceselect to the first drop-down list. change (function () {// you need to clear the city drop-down list for each request. Otherwise, the city drop-down list will be accumulated // deleted, except for $ cityselect. find ("option: Not (: First )"). remove (); // Delete the county drop-down list <option>, except for $ countyselect. find ("option: Not (: First )"). remove (); var pid = This. value; If (pid = "") return; $. post (".. /cityservlet ", {pid: PID}, function (jsonarray) {showcitys (jsonarray) ;}," JSON ") ;}); // display Level 3, add a click RESPONSE event $ cityselect to the drop-down list of level 2. change (function () {var cid = This. value; $. post (".. /countyservlet ", {CID: Cid}, function (jsonarray) {showcountys (jsonarray) ;}," JSON ") ;}); // display the province function showprovinces (jsonarray) {$. each (jsonarray, function () {var pid = This. PID; var pname = This. pname; $ provinceselect. append ('<option value = "' + PID + '">' + pname + '</option>') ;}// display the city function showcitys (jsonarray) {$. each (jsonarray, function () {var cid = This. CID; var cname = This. cname; $ cityselect. append ('<option value = "' + CID + '">' + cname + '</option>') ;}// display the county function showcountys (jsonarray) {$. each (jsonarray, function () {var tid = This. TID; var tname = This. tname; $ countyselect. append ('<option value = "' + TID + '">' + tname + '</option> ');});}});



3. servlet

public class ProvinceServlet extends HttpServlet {private static final long serialVersionUID = 1L;private ProvinceDao dao=new ProvinceDao();protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {List<Province> list = dao.getAll();String json=new Gson().toJson(list);System.out.println(json);response.setContentType("text/json;charset=utf-8");response.getWriter().write(json);}}
public class CityServlet extends HttpServlet {private static final long serialVersionUID = 1L;private CityDao cityDao=new CityDao();protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String pid = request.getParameter("pid");List<City> list = cityDao.getListByPid(pid);String json=new Gson().toJson(list);System.out.println(json);response.setContentType("text/json;charset=utf-8");response.getWriter().write(json);}}

public class CountyServlet extends HttpServlet {private static final long serialVersionUID = 1L;private CountyDao countyDao=new CountyDao();protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("11111111");String cid = request.getParameter("cid");System.out.println(cid);List<County> list = countyDao.getListByCid(cid);String json=new Gson().toJson(list);System.out.println(json);response.setContentType("text/json;charset=utf-8");response.getWriter().write(json);}}


4. DAO and bean


Three-Level 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.