layui省市區三級聯動插件

這個是一個layui省市區三級聯動插件,近來使用layui後臺模板框架開發後臺系統,要用到省市縣/區三級聯動,本想從網上找個現成的輪子,然沒有自己想要的需求,就自己造了個。

本插件實現省市區相應數據通過ajax動態從服務端加載數據,省市區數據結構key-value形式。

html示例:

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<link rel="stylesheet" href="./layui/css/layui.css" />
	</head>
	<body>
		<div class="layui-form">
			<div class="layui-input-inline">
				<select name="province" lay-filter="province" class="province">
					<option value="">請選擇省</option>
				</select>
			</div>
			<div class="layui-input-inline">
				<select name="city" lay-filter="city" disabled>
					<option value="">請選擇市</option>
				</select>
			</div>
			<div class="layui-input-inline">
				<select name="area" lay-filter="area" disabled>
					<option value="">請選擇縣/區</option>
				</select>
			</div>
		</div>
	</body>
	<script type="text/javascript" src="./layui/layui.js"></script>
	<script type="text/javascript">
		layui.config({
			base : "./layuiarea/" //layuiarea.js的路徑
		}).use([ 'layer', 'jquery', "layuiarea" ], function() {
			var layer = layui.layer, $ = layui.jquery, layuiarea = layui.layuiarea(); 
		});
	</script>
<html>

說明: 如只想顯示二級聯動,可將以下代碼註釋

			<div class="layui-input-inline">
				<select name="area" lay-filter="area" disabled>
					<option value="">請選擇縣/區</option>
				</select>
			</div>

js示例

layui.define(["form","jquery"],function(exports){
    var form = layui.form,
        $ = layui.jquery,
        Layuiarea = function(){};

    Layuiarea.prototype.provinces = function() {
        //加載省數據
        var proHtml = '',that = this;
        //這裏更換成你自己的url
        $.get("https://xxxxxxx/test2/area.php?type=0&id=0", function (data) {

            var dataObj = eval(data);
            $.each(dataObj,function(idx,item){
                proHtml += '<option value="' + item.code + '">' + item.name + '</option>';
            });

            //初始化省數據
            $("select[name=province]").append(proHtml);
            form.render();//更新  所在容器內的全部表單狀態
            form.on('select(province)', function (proData) {
                $("select[name=city]").html('<option value="">請選擇市</option>');
                var value = proData.value;
                if (value > 0) {
                //這裏更換成你自己的url
                    $.get("https://xxxxxxxx/test2/area.php?type=1&id="+value, function (data) {
                        var ciHtml='';
                        var dataObj = eval(data);
                        $.each(dataObj,function(idx,item){
                            ciHtml += '<option value="' + item.code + '">' + item.name + '</option>';
                        });
                        //加載市
                        $("select[name=city]").append(ciHtml).removeAttr("disabled");
                        form.render();
                    });
                } else {
                    $("select[name=city]").attr("disabled", "disabled");
                }
            });

            form.on('select(city)', function (cityData) {
                $("select[name=area]").html('<option value="">請選擇縣/區</option>');
                var value = cityData.value;
                if (value > 0) {
                   //這裏更換成你自己的url
                    $.get("https://xxxxxxxxx/test2/area.php?type=2&id="+value, function (data) {
                        var areaHtml='';
                        var dataObj = eval(data);
                        $.each(dataObj,function(idx,item){
                            areaHtml += '<option value="' + item.code + '">' + item.name + '</option>';
                        });
                        //加載縣區
                        $("select[name=area]").append(areaHtml).removeAttr("disabled");
                        form.render();
                    });
                } else {
                    $("select[name=area]").attr("disabled", "disabled");
                }
            });
        })
    }

    var layuiarea = new Layuiarea();
    exports("layuiarea",function(){
        layuiarea.provinces();
    });
})

php示例

<?php
$id = $_GET['id'] ?? 0;
$type = $_GET['type'] ?? 0;//
if(!$type){
    $data = [0 => [['code' => 110000, 'name' => '北京'],
        //其他省...
        ['code' => 130000, 'name' => "河北"],],
        130100=>[['code' => 130102, 'name' => '長安區'],],
        110000=>[['code' => 110101, 'name' => '東城區'],],
        130400=>[['code' => 130402, 'name' => '邯山區'],],
    ];
}elseif($type==1){
    //市
    $data = [ 110000 => [['code' => 110000, 'name' => "北京"]],
        130000 => [['code' => 130100, 'name' => '石家莊'], ['code' => 130400, 'name' => "邯鄲"]],
        ];
}else{
    //區
    $data = [
        130100=>[['code' => 130102, 'name' => '長安區'],],
        110000=>[['code' => 110101, 'name' => '東城區'],],
        130400=>[['code' => 130402, 'name' => '邯山區'],],
    ];
}
echo json_encode($data[$id], JSON_UNESCAPED_UNICODE);

預覽地址:https://demo.duiniya.com/test2/demo.html
項目地址:https://github.com/guyan0319/layuiarea
如有任何問題或建議,歡迎批評指正!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章