温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

js如何实现省市区三级联动非select下拉框版

发布时间:2021-09-24 10:49:33 来源:亿速云 阅读:124 作者:小新 栏目:开发技术

这篇文章主要介绍了js如何实现省市区三级联动非select下拉框版,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

效果

js如何实现省市区三级联动非select下拉框版

js如何实现省市区三级联动非select下拉框版

js如何实现省市区三级联动非select下拉框版

代码如下,小白写的有点乱,大家想看的就看看

1.html代码

<div class="box">
        <section class="province">省</section>
        <section class="city">市</section>
        <section class="area">区</section>
 
        <div class="si">
        </div>
</div>

2.css代码

<style>
        .box{
            width: 800px;
            height: 50px;
            margin: 20px auto;
            background-color: rgb(48, 49, 48);
            border-radius: 10px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .box section{
            display: inline-block;
            background-color: rgb(168, 165, 165);
            flex-grow: 1;
            height: 30px;
            margin-right: 10px;
            margin-left: 10px;
            border-radius: 5px;
            line-height: 30px;
            padding-left: 10px;
        }
        .box section:hover{
            cursor: pointer;
        }
        .si{
            display: none;
        }
        .box .all{
            display: block;
            width: 740px;
            background-color: rgb(211, 203, 203);
            padding: 20px;
            position: absolute;
            border-radius: 10px;
            top: 57px;
        }
        .box .all:hover{
            cursor: pointer;
        }
        .box .all span{
            display: inline-block;
            width: 130px;
            height: 30px;
            font-size: 13px;
            padding-left: 10px;
            line-height: 30px;
            border-radius: 5px;
            margin-left: 10px;
            border: 1px solid #000;
            background-color: white;
            box-sizing: border-box;
            margin-top: 10px;
        }
</style>

3.js代码

<script>
        // 获取内容
        var data = city_code// 这个是我的数据
        // 获取省市区
        var province = document.querySelector(".province")
        var city = document.querySelector(".city")
        var area = document.querySelector(".area")
 
        // 获取隐藏div
        var si = document.querySelector(".si")
        
        province.addEventListener("mouseover",function(){
            si.classList.add("all")
            // 选择省
            var html = ""
            var all = document.querySelector(".all")
            for(var i = 0;i<data.length;i++){
                const provinceName = data[i].name
                const provinceID = data[i].code
                html += `<span id="${provinceID}">${provinceName}</span>`
            }
            all.innerHTML=html
 
            var spanAll = document.querySelectorAll("span")
            for(var j =0;j<spanAll.length;j++){
                spanAll[j].addEventListener("click",function(){
                    province.innerHTML=this.innerText
                    province.id=this.id
 
                    html = ""
 
                    // 选择市
                    for(var k = 0;k<data.length;k++){
                if (data[k].code===province.id) {
                    var citys = data[k].city
                    for(var i = 0;i<citys.length;i++){
                    html +=`<span id="${citys[i].code}">${citys[i].name}</span>`
                    }
                    all.innerHTML=html
                    var spanAll = document.querySelectorAll("span")
                    for(var j =0;j<spanAll.length;j++){
                        spanAll[j].addEventListener("click",function(){
                        city.innerHTML=this.innerText
                        city.id=this.id
 
                        html = ""
 
                        //选择区
                        for(var k = 0;k<citys.length;k++){
                if (citys[k].code===city.id) {
                    var areas = citys[k].area
                    for(var i = 0;i<areas.length;i++){
                    html +=`<span id="${areas[i].code}">${areas[i].name}</span>`
                    }
                    all.innerHTML=html
                    var spanAll = document.querySelectorAll("span")
                    for(var j =0;j<spanAll.length;j++){
                        spanAll[j].addEventListener("click",function(){
                        area.innerHTML=this.innerText
                        area.id=this.id
                        si.classList.remove("all")                 
                        })               
                    }
                    break
                }
            }
 
                        })                
                    }
                    break
                }
            }    
 
 
                         
                })                
            }
            
 
        })
 
</script>

感谢你能够认真阅读完这篇文章,希望小编分享的“js如何实现省市区三级联动非select下拉框版”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js
AI