• 案例:Ajax实现省市联动,选择省后动态显示市和区


    前端:

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>省市联动title>
    head>
    <body>
    <script src="jquery.js" type="text/javascript">script>
    <script type="text/javascript">
        $(function () {
            let provinceSelect = $("#province");
            let citySelect = $("#city");
            let countrySelect = $("#country");
            //浏览器加载完毕发送ajax请求,获取所有省份到下拉列表
            $.ajax({
                dataType:"json",
                type:"get",
                url:"/xmm/province",
                async:true,
                data:"t="+new Date(),
                success:function (json) {
                    let html = "";
                    for (let i = 0; i < json.length; i++) {
                        myJson = json[i];
                        html += "+myJson.cityname+">";
                    }
                    provinceSelect.html(html);
                }
            })
            //省下拉框选择完毕后执行该事件,获取市
            provinceSelect[0].onchange = function () {
                $.ajax({
                    dataType:"json",
                    type:"get",
                    url:"/xmm/province",
                    data:"t="+new Date()+"&id="+provinceSelect[0].value,
                    async:true,
                    success:function (json) {
                        let html = "";
                        for (let i = 0; i < json.length; i++) {
                            myJson = json[i];
                            html += "+myJson.cityname+">";
                        }
                        citySelect.html(html);
                    }
                })
            }
            //市下拉列表框选择完毕后执行该事件,获取区
            citySelect[0].onchange = function () {
                $.ajax({
                    dataType:"json",
                    type:"get",
                    url:"/xmm/province",
                    data:"t="+new Date()+"&id="+this.value,
                    async:true,
                    success:function (json) {
                        let html = "";
                        for (let i = 0; i < json.length; i++) {
                            myJson = json[i];
                            html += "+myJson.cityname+">";
                        }
                        countrySelect.html(html);
                    }
                })
            }
        })
        
        
    script>
    请选择所在省:
    <select id="province">
        <option value="">---请选择省份---option>
    select><br/>
    请选择所在市:
    <select id="city">
        <option value="">---请选择所在市---option>
    select><br/>
    请选择所在区:
    <select id="country">
        <option value="">---请选择所在直辖区---option>
    select><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    后端:

    package provinceAndcity;
    
    import com.alibaba.fastjson.JSON;
    import jakarta.servlet.ServletException;
    import jakarta.servlet.annotation.WebServlet;
    import jakarta.servlet.http.HttpServlet;
    import jakarta.servlet.http.HttpServletRequest;
    import jakarta.servlet.http.HttpServletResponse;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.ResourceBundle;
    @WebServlet("/province")
    public class ProvinceServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            ResourceBundle rb = ResourceBundle.getBundle("jdbc");
            Connection con = null;
            PreparedStatement pre = null;
            ResultSet rs = null;
            List<ProvinceJSON> list = new ArrayList();
            try {
                Class.forName(rb.getString("driver"));
                con = DriverManager.getConnection(rb.getString("url"),rb.getString("username"),rb.getString("password"));
                if (request.getParameter("id") == null){//查询省
                    pre = con.prepareStatement("select cityname, id from city where type='1'");
                }else{//根据前端返回值查询市区
                    pre = con.prepareStatement("select cityname, id from city where pid= ? ");
                    pre.setString(1,request.getParameter("id"));
                }
                rs = pre.executeQuery();
                while(rs.next()){
                    int id = rs.getInt("id");
                    String cityname = rs.getString("cityname");
                    ProvinceJSON province = new ProvinceJSON(id,cityname);
                    list.add(province);
                }
                String jsons = JSON.toJSONString(list);
                out.print(jsons);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (rs != null) {
                    try {
                        rs.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (pre != null) {
                    try {
                        pre.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
                if (con != null) {
                    try {
                        con.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
  • 相关阅读:
    Numpy-01(安装、Ndarray对象介绍创建及使用)
    02 ts 变量定义,类型
    【华为OD机试python】报文回路【2023 B卷|100分】
    初次选购云服务器宽带如何选择?3M够吗?
    你被MySQL 中的反斜杠 \\坑过吗?
    python re 使用非捕获组来忽略第一个value的匹配结果
    快速查看物理机器CPU核数
    代码随想录算法训练营第六天 |18. 四数之和、344.反转字符串、541. 反转字符串II
    mulesoft Module 11 quiz解析
    【JavaSE】数据类型与变量
  • 原文地址:https://blog.csdn.net/m0_53881899/article/details/126713269