主頁(yè) > 知識(shí)庫(kù) > Ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)

Ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)

熱門標(biāo)簽:提高電話機(jī)器人接通率 外呼系統(tǒng)api對(duì)接 荊州智能電銷機(jī)器人 廣西智能外呼系統(tǒng)多少錢 大學(xué)校門地圖標(biāo)注 福建微碼電話機(jī)器人 平?jīng)龈叩碌貓D標(biāo)注商戶要收費(fèi)嗎 銷售電銷機(jī)器人詐騙 地圖標(biāo)注與公司業(yè)務(wù)關(guān)系

本文實(shí)例為大家分享了Ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下

首先建立數(shù)據(jù)庫(kù),如下所示

接口

import java.util.List;
public interface ProvinceDao {
 ListProvince> findAll();
}

import java.util.List;
public interface CityDao {
 ListCity> findCityByPid(int pid);
}

import java.util.List;
public interface AreaDao {
 ListArea> findAreaByCid(int cid);
}

接口實(shí)現(xiàn)類

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProvinceDaoImpl implements ProvinceDao{
 public ListProvince> findAll(){
 Connection conn = DBHelper.getConn();
 ArrayListProvince> provinces = new ArrayListProvince>();
 String sql = "select * from aprovince";
 try {
 PreparedStatement ps = conn.prepareStatement(sql);
 ResultSet rs = ps.executeQuery();
 while (rs.next()){
 Province p = new Province();
 p.setPid(rs.getInt(1));
 p.setPname(rs.getString(2));
 provinces.add(p);
 }
 } catch (SQLException e) {
 e.printStackTrace();
 }
 return provinces;
 }
}

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CityDaoImpl implements CityDao {
 @Override
 public ListCity> findCityByPid(int pid) {
 Connection conn = DBHelper.getConn();

 ArrayListCity> cities = new ArrayList>();

 String sql = "select * from acity where pid=?";

 try {
 PreparedStatement ps = conn.prepareStatement(sql);
 ps.setInt(1,pid);
 ResultSet rs = ps.executeQuery();
 while (rs.next()){
 City city = new City();
 city.setPid(rs.getInt(3));
 city.setCid(rs.getInt(1));
 city.setCname(rs.getString(2));
 cities.add(city);
 }
 } catch (SQLException e) {
 e.printStackTrace();
 }
 return cities;
 }
}

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AreaDaoImpl implements AreaDao {
 @Override
 public ListArea> findAreaByCid(int cid) {
 Connection conn = DBHelper.getConn();
 ArrayListArea> areas = new ArrayList>();
 String sql = "select * from aarea where cid=?";

 try {
 PreparedStatement ps = conn.prepareStatement(sql);
 ps.setInt(1,cid);
 ResultSet rs = ps.executeQuery();
 while (rs.next()){
 Area area = new Area();
 area.setCid(rs.getInt(3));
 area.setAid(rs.getInt(1));
 area.setAname(rs.getString(2));
 areas.add(area);
 }
 } catch (SQLException e) {
 e.printStackTrace();
 }
 return areas;
 }
}

servlet

package cn.zhc.servlet;

import cn.zhc.dao.Impl.ProvinceDaoImpl;
import cn.zhc.dao.ProvinceDao;
import cn.zhc.domin.Province;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/findAll")
public class FindAll extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 request.setCharacterEncoding("utf-8");
 response.setContentType("text/html;charset=utf-8");

 ProvinceDao provinceDao = new ProvinceDaoImpl();
 ListProvince> lists=provinceDao.findAll();

 response.getWriter().write(JSONObject.toJSONString(lists));
 }
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 this.doPost(request, response);
 }
}

package cn.zhc.servlet;

import cn.zhc.dao.CityDao;
import cn.zhc.dao.Impl.CityDaoImpl;
import cn.zhc.domin.City;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/findCityByPid")
public class FindCityByPid extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 request.setCharacterEncoding("utf-8");
 response.setContentType("text/html;charset=utf-8");

 String pid = request.getParameter("pid");

 CityDao cityDao = new CityDaoImpl();
 ListCity> cityList = cityDao.findCityByPid(Integer.parseInt(pid));

 response.getWriter().write(JSONObject.toJSONString(cityList));
 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 this.doPost(request, response);
 }
}

package cn.zhc.servlet;

import cn.zhc.dao.AreaDao;
import cn.zhc.dao.Impl.AreaDaoImpl;
import cn.zhc.domin.Area;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/findAreaByCid")
public class FindAreaByCid extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 request.setCharacterEncoding("utf-8");
 response.setContentType("text/html;charset=utf-8");

 String cid = request.getParameter("cid");

 AreaDao areaDao = new AreaDaoImpl();
 ListArea> areas = areaDao.findAreaByCid(Integer.parseInt(cid));

 response.getWriter().write(JSONObject.toJSONString(areas));
 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 this.doPost(request, response);
 }
}

JSP頁(yè)面

%@ page contentType="text/html;charset=UTF-8" language="java" %>
html>
head>
 title>三級(jí)聯(lián)動(dòng)/title>
 script type="text/javascript" src="js/jquery-1.8.3.js">/script>
/head>
body>
script type="text/javascript">
 $(function () {
 $.ajax({
 type:"get",
 url:"findAll",
 dataType:"json",
 success:function (data) {
 var obj=$("#province");
 for(var i=0;idata.length;i++){
 var ob="option value='"+data[i].pid+"'>"+data[i].pname+"/option>";
 obj.append(ob);
 }
 }
 })

 $("#province").change(function () {
 $("#city option").remove();
 $.ajax({
 type:"get",
 async:false,
 url:"findCityByPid?pid="+$("#province").val(),
 dataType:"json",
 success:function (data) {
 var obj=$("#city");
 for(var i=0;idata.length;i++){
 var ob="option value='"+data[i].cid+"'>"+data[i].cname+"/option>";
 obj.append(ob);
 }
 }
 })
 });

 $("#city,#province").change(function () {
 $("#area option").remove();
 $.ajax({
 type:"get",
 async:false,
 url:"findAreaByCid?cid="+$("#city").val(),
 dataType:"json",
 success:function (data) {
 var obj=$("#area");
 for(var i=0;idata.length;i++){
 var ob="option value='"+data[i].aid+"'>"+data[i].aname+"/option>";
 obj.append(ob);
 }
 }
 })
 });
 });
/script>
select name="province" id="province">
 option value="0">請(qǐng)選擇/option>
/select>省
select name="city" id="city">
 option value="0">請(qǐng)選擇/option>
/select>市
select name="area" id="area">
 option value="0">請(qǐng)選擇/option>
/select>縣
/body>
/html>

實(shí)現(xiàn)結(jié)果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • jQuery ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)
  • ajax實(shí)現(xiàn)無(wú)刷新省市縣三級(jí)聯(lián)動(dòng)
  • AJAX和WebService實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)具體代碼

標(biāo)簽:邯鄲 樂(lè)山 衡陽(yáng) 內(nèi)江 婁底 德陽(yáng) 海南 黔東

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)》,本文關(guān)鍵詞  Ajax,實(shí)現(xiàn)省,市縣,三級(jí),聯(lián)動(dòng),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Ajax實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章