zoukankan      html  css  js  c++  java
  • 省市区联动

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>省市区联动</title>
        <link rel="stylesheet" href="/assets/bootstrap/dist/css/bootstrap.min.css">
        <style type="text/css">
            .container {
                padding-top: 150px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="form-inline">
                <div class="form-group">
                    <select class="form-control" id="province"></select>
                </div>
                <div class="form-group">
                    <select class="form-control" id="city">
                        <option>请选择城市</option>
                    </select>
                </div>
                <div class="form-group">
                    <select class="form-control" id="area">
                        <option>请选择县城</option>
                    </select>
                </div>
            </div>
        </div>
        <script src="/js/ajax.js"></script>
        <script src="/js/template-web.js"></script>
        <!-- 省份模板 -->
        <script type="text/html" id="provinceTpl">
            <option>请选择省份</option>
            {{each province}}
                <option value="{{$value.id}}">{{$value.name}}</option>
            {{/each}}
        </script>
        <!-- 城市模板 -->
        <script type="text/html" id="cityTpl">
            <option>请选择城市</option>
            {{each city}}
                <option value="{{$value.id}}">{{$value.name}}</option>
            {{/each}}
        </script>
        <!-- 县城模板 -->
        <script type="text/html" id="areaTpl">
            <option>请选择县城</option>
            {{each area}}
                <option value="{{$value.id}}">{{$value.name}}</option>
            {{/each}}
        </script>
        <script>
            // 获取省市区下拉框元素
            var province = document.getElementById('province');
            var city = document.getElementById('city');
            var area = document.getElementById('area');
            // 获取省份信息
            ajax({
                type: 'get',
                url: 'http://localhost:3000/province',
                success: function (data) {
                    // 将服务器端返回的数据和html进行拼接
                    var html = template('provinceTpl', {province: data});
                    // 将拼接好的html字符串显示在页面中
                    province.innerHTML = html;
                }
            });

            // 为省份的下拉框添加值改变事件
            province.onchange = function () {
                // 获取省份id
                var pid = this.value;

                // 清空县城下拉框中的数据
                var html = template('areaTpl', {area: []});
                area.innerHTML = html;

                // 根据省份id获取城市信息
                ajax({
                    type: 'get',
                    url: '/cities',
                    data: {
                        id: pid
                    },
                    success: function (data) {
                        var html = template('cityTpl', {city: data});
                        city.innerHTML = html;
                    }
                })
            };

            // 当用户选择城市的时候
            city.onchange = function () {
                // 获取城市id
                var cid = this.value;
                // 根据城市id获取县城信息
                ajax({
                    type: 'get',
                    url: 'http://localhost:3000/areas',
                    data: {
                        id: cid
                    },
                    success: function(data) {
                        var html = template('areaTpl', {area: data});
                        area.innerHTML = html;
                    }
                })
            }
        </script>
    </body>
    </html>
  • 相关阅读:
    eIQ WSL下工具及环境配置
    WSL配置高翔vslam环境配置流水账
    机器学习原理/模型/应用
    Spring+Quartz(定时任务)
    vim常用操作
    Linux使用ssh公钥实现免密码登录Linux
    svn常用操作
    Jquery Html方法失效的问题
    运算符&&与||的用法
    CSS强制不换行[转帖]
  • 原文地址:https://www.cnblogs.com/ericblog1992/p/13130554.html
Copyright © 2011-2022 走看看