自己写的省市区三级联动(简洁)

通过json文件把省市区信息依次添加到多选框中,实现三级联动的效果(样式比较简单,实际应用中可以修改)

代码如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        #ZQjfamily {
            position: relative;
            display: flex;
        }

        #ZQjfamily>select {
            display: block;
            padding: 10px 54px;
            border-radius: 25%;
            background-color: rgb(0, 0, 0, 0);
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
            z-index: 999999;
            margin-right: 10px;
        }

        #ZQjfamily>select:focus {
            outline: none;
        }

        #ZQjfamily>.ZQjhuakuai {
            border-radius: 25%;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
            width: 1px;
            height: 39px;
            position: absolute;
            background-color: rgb(152, 183, 85);
            left: 0px;
            transition: all .5s linear;
            top: 1px;
            z-index: 99;
        }
    </style>
</head>

<body>
    <div id="ZQjfamily">
        <div class="ZQjhuakuai"></div>
        <select name="" class="active" id="provinceZQj">
            <option data-index="moren">请选择省</option>
        </select>
        <select name="" id="cityZQJ">
            <option disabled selected>请先选择省</option>
        </select>
        <select name="" id="districtZQJ">
            <option disabled selected>请先选择省和城市</option>
        </select>
    </div>
</body>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
    $(".ZQjhuakuai").css("width", $('.active').css('width'));
    $('#ZQjfamily').on('mouseenter', 'select', function (e) {
        $('.ZQjhuakuai').css("left", e.currentTarget.offsetLeft + "px");
        $('.ZQjhuakuai').css("width", $(this).css("width"))
    })
    $('#ZQjfamily').on('mouseleave', 'select', function (e) {
        $(".ZQjhuakuai").css("left", $('.active')[0].offsetLeft + "px");
        $('.ZQjhuakuai').css("width", $('.active').css('width'))
    })
    $("#ZQjfamily").on("click", 'select', function (e) {
        $("select").removeClass("active");
        $(this).addClass("active");
        $('.ZQjhuakuai').css("width", $(this).css("width"))
        $(".ZQjhuakuai").css("left", e.currentTarget.offsetLeft + "px");
    })
    var information = null
    var cityList = null
    var cityIndex = null
    $.getJSON('./sanji.json', function (data) {
        information = data
        $.each(data, function (index, value) {
            var provinceZQj = document.createElement('option')
            $(provinceZQj).text(value.value)
            $(provinceZQj).val(value.value)
            $(provinceZQj).attr('data-index', index)
            $('#provinceZQj').append(provinceZQj)
        })
    })
    $('#provinceZQj').change(function () {
        $('#cityZQJ').html()
        $('#cityZQJ').html('<option disabled selected>请先选择省</option>')
        $('#districtZQJ').html()
        $('#districtZQJ').html('<option disabled selected>请先选择省和城市</option>')
        $('#cityZQJ>option').remove(0)
        const provinceIndex = $(this).children("option:selected").attr('data-index')
        cityList = information[provinceIndex]['child']
        console.log(cityList);
        $.each(cityList, function (index, value) {
            var cityZQJ = document.createElement('option')
            $(cityZQJ).text(value.value)
            $(cityZQJ).val(value.value)
            $(cityZQJ).attr('data-index', index)
            $('#cityZQJ').append(cityZQJ)
        })
        cityIndex = $('#cityZQJ').children("option:selected").attr('data-index')
        var districtList = cityList[cityIndex]['child']
        $('#districtZQJ>option').remove(0)
        $.each(districtList, function (index, value) {
            var districtZQJ = document.createElement('option')
            $(districtZQJ).text(value.value)
            $(districtZQJ).val(value.value)
            console.log(districtZQJ);
            $('#districtZQJ').append(districtZQJ)
        })
    })
    $('#cityZQJ').change(function () {
        $('#districtZQJ').html()
        $('#districtZQJ').html('<option disabled selected>请先选择省和城市</option>')
        $('#districtZQJ>option').remove(0)
        cityIndex = $(this).children("option:selected").attr('data-index')
        var districtList = cityList[cityIndex]['child']
        $.each(districtList, function (index, value) {
            var districtZQJ = document.createElement('option')
            $(districtZQJ).text(value.value)
            $(districtZQJ).val(value.value)
            $('#districtZQJ').append(districtZQJ)
        })
    })
</script>

</html>
添加了移入移出动画效果,不要可以删掉…

预览效果效果https://mrszqj.github.io/ccity/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值