MENU

layui实现省市区联动、4位编码和6位编码

• June 26, 2020 • Read: 2328 • 折腾阅读设置

接某api,要求提供四位地址编码和六位地址编码。

于是整理了一下,记录一下。

基于layui实现的,效果还不错。

6位地区编码代码如下:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>省市区 6位</title>
    
    <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
    
    <script src="layui/layui.js"></script>
    
    <link rel="stylesheet" href="layui/css/layui.css">
    
    <!--省市区6位编码文件-->
    <script src="js/shengshiqu6.js"></script>
    
    <script type="text/javascript">
        var defaults = {
            s1: 'province_code',
            s2: 'city_code',
            s3: 'district_code',
            v1: null,
            v2: null,
            v3: null
        };
        var $form;
        var form;
        var $;
        layui.define(['jquery', 'form'], function () {
            $ = layui.jquery;
            form = layui.form;
            $form = $('form');
            treeSelect(defaults);
        });
        function treeSelect(config) {
            config.v1 = config.v1 ? config.v1 : 110000;
            config.v2 = config.v2 ? config.v2 : 110100;
            config.v3 = config.v3 ? config.v3 : 110101;
            $.each(threeSelectData, function (k, v) {
                appendOptionTo($form.find('select[name=' + config.s1 + ']'), k, v.val, config.v1);
            });
            form.render();
            cityEvent(config);
            areaEvent(config);
            form.on('select(' + config.s1 + ')', function (data) {
                cityEvent(data);
                form.on('select(' + config.s2 + ')', function (data) {
                    areaEvent(data);
                });
            });
 
            function cityEvent(data) {
                $form.find('select[name=' + config.s2 + ']').html("");
                config.v1 = data.value ? data.value : config.v1;
                $.each(threeSelectData, function (k, v) {
                    if (v.val == config.v1) {
                        if (v.items) {
                            $.each(v.items, function (kt, vt) {
                                appendOptionTo($form.find('select[name=' + config.s2 + ']'), kt, vt.val, config.v2);
                            });
                        }
                    }
                });
                form.render();
                config.v2 = $('select[name=' + config.s2 + ']').val();
                areaEvent(config);
            }
            function areaEvent(data) {
                $form.find('select[name=' + config.s3 + ']').html("");
                config.v2 = data.value ? data.value : config.v2;
                $.each(threeSelectData, function (k, v) {
                    if (v.val == config.v1) {
                        if (v.items) {
                            $.each(v.items, function (kt, vt) {
                                if (vt.val == config.v2) {
                                    $.each(vt.items, function (ka, va) {
                                        appendOptionTo($form.find('select[name=' + config.s3 + ']'), ka, va, config.v3);
                                    });
                                }
                            });
                        }
                    }
                });
                form.render();
                form.on('select(' + config.s3 + ')', function (data) { });
            }
            function appendOptionTo($o, k, v, d) {
                var $opt = $("<option>").text(k).val(v);
                if (v == d) { $opt.attr("selected", "selected") }
                $opt.appendTo($o);
            }
        }
    </script>
</head>
<body>
<div style="width:800px;margin:50px auto;">
    <form class="layui-form" method="get" action="f.php">
        
        <div class="layui-form-item">
            <label class="layui-form-label">选择地区</label>
            <div class="layui-input-inline">
                <select name="province_code" id="province_code" lay-filter="province_code">
                    <option value="">请选择省</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="city_code" id="city_code" lay-filter="city_code">
                    <option value="">请选择市</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="district_code" id="district_code" lay-filter="district_code">
                    <option value="">请选择县/区</option>
                </select>
            </div>
        </div>
        <button type="submit" class="layui-btn layui-btn-fluid layui-btn-normal">GET提交测试</button>
    </form>
</div>
</body>
</html>

4位地区编码代码如下:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>省市四位</title>
    
    <script type="text/javascript" src="http://code.jquery.com/jquery.js"></script>
    
    <script src="layui/layui.js"></script>
    
    <link rel="stylesheet" href="layui/css/layui.css">
    
    <!--省市区4位编码文件-->
    <script src="js/shengshiqu4.js"></script>
    
    <script type="text/javascript">
    /**layui.use('form', function(){
        var form = layui.form;
    });*/
    </script>
    
    <script type="text/javascript">
        var defaults = {
            s1: 'prov_code',
            s2: 'area_code',
            v1: null,
            v2: null
        };
        var $form;
        var form;
        var $;
        layui.define(['jquery', 'form'], function () {
            $ = layui.jquery;
            form = layui.form;
            $form = $('form');
            treeSelect(defaults);
        });
        function treeSelect(config) {
            config.v1 = config.v1 ? config.v1 : 0011;
            config.v2 = config.v2 ? config.v2 : 1100;
            $.each(twoData, function (k, v) {
                appendOptionTo($form.find('select[name=' + config.s1 + ']'), v.title, v.value, config.v1);
            });
            form.render();
            cityEvent(config);
            form.on('select(' + config.s1 + ')', function (data) {
                cityEvent(data);
            });
         
            function cityEvent(data) {
                $form.find('select[name=' + config.s2 + ']').html("");
                config.v1 = data.value ? data.value : config.v1;
                $.each(twoData, function (k, v) {
                    if (v.value == config.v1) {
                        if (v.cities) {
                            $.each(v.cities, function (kt, vt) {
                                appendOptionTo($form.find('select[name=' + config.s2 + ']'), vt.title, vt.value, config.v2);
                            });
                        }
                    }
                });
                form.render();
                config.v2 = $('select[name=' + config.s2 + ']').val();
            }
            
            function appendOptionTo($o, k, v, d) {
                var $opt = $("<option>").text(k).val(v);
                if (v == d) { $opt.attr("selected", "selected") }
                $opt.appendTo($o);
            }
        }
    </script>
    
</head>
<body>
<div style="width:800px;margin:50px auto;">
    <form class="layui-form" method="get" action="f.php">
        
        <div class="layui-form-item">
            <label class="layui-form-label" >省市编码(4)</label>
            <div class="layui-input-inline">
                <select name="prov_code" id="prov_code" lay-filter="prov_code">
                    <option value="">请选择省</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="area_code" id="area_code" lay-filter="area_code">
                    <option value="">请选择市</option>
                </select>
            </div>
        </div>
        <button type="submit" class="layui-btn layui-btn-fluid layui-btn-normal">GET提交测试</button>
    </form>
</div>
</body>
</html>

代码打包:省市区-4位-6位编码.zip

Last Modified: June 21, 2023
Archives QR Code Tip
QR Code for this page
Tipping QR Code