Ajax实现省市县三级联动_AJAX相关

来源:脚本之家  责任编辑:小易  

本文实例为大家分享了Ajax实现省市县三级联动的具体代码,供大家参考,具体内容如下

首先建立数据库,如下所示

接口

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

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

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

接口实现类

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 List<Province> findAll(){
 Connection conn = DBHelper.getConn();
 ArrayList<Province> provinces = new ArrayList<Province>();
 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 List<City> findCityByPid(int pid) {
 Connection conn = DBHelper.getConn();

 ArrayList<City> 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 List<Area> findAreaByCid(int cid) {
 Connection conn = DBHelper.getConn();
 ArrayList<Area> 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();
 List<Province> 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();
 List<City> 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();
 List<Area> 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>三级联动</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;i<data.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;i<data.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;i<data.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>

实现结果如下:


  • 本文相关:
  • ajax异步检查用户名是否存在
  • 关于ajax对象一些常用属性、事件和方法大小写比较常见的问题总结
  • ajax.js里面有内容显示效果,根据id
  • 基于ajax实现无刷新分页的方法
  • ajax 动态加载后台数据 绑定select的方法
  • ajax提交session超时跳转页面使用全局的方法来处理
  • ajax实现局部刷新的方法实例
  • jquery中ajax - get() 方法实例详解
  • 如何解决ajax访问不断变化的session的值不一致以及http协议中的g
  • ajax实现服务器与浏览器长连接的功能
  • 使用AJAX实现省份和城市的级联显示
  • 用c#怎样实现省市县三级联动
  • 求这种形式的三级联动后台数据怎么读取
  • 我用的是JS实现的省市县三级联动,但是存入数据库里的是他们的所属ID,如何用MYSQL语句提取呢?百度知道
  • 如何实现select省市地区三级联动并回显
  • 省市联动有用ajax的必要么
  • 网站首页网页制作脚本下载服务器操作系统网站运营平面设计媒体动画电脑基础硬件教程网络安全javascriptasp.netphp编程ajax相关正则表达式asp编程jsp编程编程10000问css/htmlflex脚本加解密web2.0xml/rss网页编辑器相关技巧安全相关网页播放器其它综合dart首页jquery ajax实现省市县三级联动ajax实现无刷新省市县三级联动ajax和webservice实现省市县三级联动具体代码ajax异步检查用户名是否存在关于ajax对象一些常用属性、事件和方法大小写比较常见的问题总结ajax.js里面有内容显示效果,根据id基于ajax实现无刷新分页的方法ajax 动态加载后台数据 绑定select的方法ajax提交session超时跳转页面使用全局的方法来处理ajax实现局部刷新的方法实例jquery中ajax - get() 方法实例详解如何解决ajax访问不断变化的session的值不一致以及http协议中的gajax实现服务器与浏览器长连接的功能jquery ajax 向后台传递数组参数ajax readystate的五种状态详解ajax中的async属性值之同步和异步jquery实现ajax定时刷新局部页面ajax传递多个参数具体实现ajax获取数据中文乱码问题最简单ajax获取数据然后显示在页面的实jquery ajax中使用serialize()方自己动手打造ajax图片上传(网上没ajax 缓存问题的两种解决方法(ieajax封装类使用指南详谈ajax返回数据成功 却进入error的方法纯js实现ajax局部刷新功能js对ajax返回数组的处理介绍探讨ajax中有关readystate(状态值)和stajax 进度条实现代码ajax 异步加载解析各种ajax方法的使用比较详解发布三个ajax相关的函数,包括无刷新提交ajax 表单验证 实现代码
    免责声明 - 关于我们 - 联系我们 - 广告联系 - 友情链接 - 帮助中心 - 频道导航
    Copyright © 2017 www.zgxue.com All Rights Reserved