佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Ajax實現(xiàn)省市縣三級聯(lián)動

Ajax實現(xiàn)省市縣三級聯(lián)動

熱門標簽:Linux服務(wù)器 阿里云 團購網(wǎng)站 服務(wù)器配置 科大訊飛語音識別系統(tǒng) 銀行業(yè)務(wù) 電子圍欄 Mysql連接數(shù)設(shè)置

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

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

接口

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

接口實現(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頁面

%@ page contentType="text/html;charset=UTF-8" language="java" %>
html>
head>
 title>三級聯(lián)動/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">請選擇/option>
/select>省
select name="city" id="city">
 option value="0">請選擇/option>
/select>市
select name="area" id="area">
 option value="0">請選擇/option>
/select>縣
/body>
/html>

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

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

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

標簽:廣元 衢州 大理 江蘇 衡水 棗莊 蚌埠 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Ajax實現(xiàn)省市縣三級聯(lián)動》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    太原市| 威海市| 商南县| 都安| 资阳市| 恩施市| 青浦区| 区。| 信丰县| 成都市| 吉林市| 皮山县| 夏津县| 加查县| 鄯善县| 元江| 潼关县| 黄石市| 东兴市| 屯昌县| 炉霍县| 连平县| 惠来县| 平舆县| 威海市| 甘孜| 疏勒县| 兴化市| 大方县| 上虞市| 监利县| 定兴县| 江油市| 河间市| 水富县| 墨江| 三江| 高阳县| 灯塔市| 体育| 临夏市|