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

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="./asset/bootstrap/dist/css/bootstrap.min.css">
        <script src="./js/ajax.js"></script>
        <script src="./js/template-web.js"></script>
    </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 type="text/html" id="protpl">
            <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.querySelector('#province')
            var city = document.querySelector('#city')
            var area = document.querySelector('#area')
            ajax({
                type: 'get',
                url: 'http://localhost:3001/province',
                success: function(data) {
                    console.log(data)
                    var html = template('protpl', {
                        province: data
                    })
                    province.innerHTML = html
                },
                error: function() {}
            })
            province.onchange = function() {
                var pid = this.value
                var html = template('areatpl', {
                    area: []
                })
                area.innerHTML = html
                ajax({
                    type: 'get',
                    url: 'http://localhost:3001/cities',
                    data: {
                        id: pid
                    },
                    success: function(data) {
                        console.log(data)
                        var html = template('citytpl', {
                            city: data
                        })
                        city.innerHTML = html
                    },
                    error: function() {}
                })
            }
            city.onchange = function() {
                var cid = this.value
                ajax({
                    type: 'get',
                    url: 'http://localhost:3001/areas',
                    data: {
                        id: cid
                    },
                    success: function(data) {
                        console.log(data)
                        var html = template('areatpl', {
                            area: data
                        })
                        area.innerHTML = html
                    },
                    error: function() {}
                })
            }
        </script>
    </body>
    
    </html>
  • 相关阅读:
    git 远程仓库 and 分支管理
    oracle 导入导出dmp
    sql if else 用法
    设置session超时的三种方式
    eclipse定制工具栏,修改工具栏
    cxf实例异常
    js 表格插入指定行
    iframe 调用父页面元素
    iframe页面刷新
    frame 和 iframe
  • 原文地址:https://www.cnblogs.com/rainbowupdate/p/13052390.html
Copyright © 2011-2022 走看看