原生JavaScript实现省级联动效果——源码

一、效果图

这里写图片描述

二、index.html

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>

    <body>

        <div>

            <select id="province" name="province" onchange="getCity(this)">
                <option value="">--请选择--</option>
            </select><select id="city" name="city" onchange="getDistrict(this)">
                <option value="">--请选择--</option>
            </select><select id="district" name="district">
                <option value="">--请选择--</option>
            </select>区/县

        </div>

        <script type="text/javascript" src="js/data.js" ></script>
        <script type="text/javascript">


            /**
             * 自调用函数,页面加载所用省份数据
             * @param {Object} provice 传入js中的变量proviceJson
             */
            (function(provice){
                // 1.禁用城市和区县乡下拉框
                var city = document.getElementById("city");
                var district = document.getElementById("district");
                city.disabled = "disabled";
                district.disabled = "disabled";
                // 2.加载所有省份
                for (var i = 0; i < provice.length; i++) {
                    var provinceOption = document.createElement("option");
                    var provinceSelect = document.getElementById("province");
                    provinceOption.innerText = provice[i].name;
                    provinceOption.value = i;//循环变量i作为option的value属性值 
                    provinceSelect.appendChild(provinceOption);
                }
            })(proviceJson);


            /**
             * 根据省份获取城市
             */
            function getCity(obj) {
                // 1.激活城市下拉框
                var citySel = obj.nextElementSibling || obj.nextSibling;
                citySel.disabled = "";
                // 2.清空城市下拉框上次缓存内容(查询结果)
                citySel.innerHTML = "";
                var cityOpt = document.createElement("option");
                cityOpt.innerText = "--请选择--";
                citySel.appendChild(cityOpt);
                // 3.清空区县乡下拉框上次缓存内容(查询结果)
                var districtSel = obj.nextElementSibling.nextElementSibling || obj.nextSibling.nextSibling;
                districtSel.innerHTML = "";
                var cityOpt = document.createElement("option");
                cityOpt.innerText = "--请选择--";
                districtSel.appendChild(cityOpt);
                // 4.获取所选省份下拉框value属性值
                var val = obj.value;
                // 5.获取省份下的城市数组
                var cityArr = proviceJson[val].city;
                for (var i = 0; i < cityArr.length; i++) {
                    var cityOption = document.createElement("option");
                    var citySelect = document.getElementById("city");
                    cityOption.innerText = cityArr[i].name;
                    cityOption.value = i; //循环变量i作为option的value属性值
                    citySelect.appendChild(cityOption);
                }
            }

            /**
             * 根据城市获取区县乡
             */
            function getDistrict(obj) {
                // 1.激活区县乡下拉框
                var districtSel = obj.nextElementSibling || obj.nextSibling;
                districtSel.disabled = "";
                // 2.获取所选省份value属性
                var proVal = (obj.previousElementSibling || obj.previousSibling).value;
                // 3.获取所选择城市下拉框value属性值
                var cityVal = obj.value;
                // 4.获取省份下的城市数组
                var districtArr = proviceJson[proVal].city[cityVal].districtAndCounty;
                for (var i = 0; i < districtArr.length; i++) {
                    var districtOption = document.createElement("option");
                    var districtSelect = document.getElementById("district");
                    districtOption.innerText = districtArr[i];
                    districtOption.value = i;//循环变量i作为option的value属性值
                    districtSelect.appendChild(districtOption);
                }
            }


        </script>

    </body>

</html>

三、data.js(模拟数据)

var proviceJson = [{
    name: "北京市",
    city: [{
        name: "北京市",
        districtAndCounty: ["东城区", "西城区", "崇文区", "宣武区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区", "昌平区", "大兴区", "怀柔区", "平谷区", "密云县", "延庆县", "延庆镇"]
    }]
}, {
    name: "重庆市",
    city: [{
        name: "重庆市",
        districtAndCounty: ["渝中区", "大渡口区", "江北区", "沙坪坝区", "九龙坡区", "南岸区", "北碚区", "万盛区", "双桥区", "渝北区", "巴南区", "万州区", "涪陵区", "黔江区", "长寿区", "合川市", "永川区市", "江津市", "南川市", "綦江县", "潼南县", "铜梁县", "大足县", "荣昌县", "璧山县", "垫江县", "武隆县", "丰都县", "城口县", "梁平县", "开县", "巫溪县", "巫山县", "奉节县", "云阳县", "忠县", "石柱土家族自治县", "彭水苗族土家族自治县", "酉阳土家族苗族自治县", "秀山土家族苗族自治县"]
    }]
}, {
    name: "河北省",
    city: [{
        name: "石家庄市",
        districtAndCounty: ["长安区", "桥东区", "桥西区", "新华区", "裕华区", "井陉矿区", "辛集市", "藁城市", "晋州市", "新乐市", "鹿泉市", "井陉县", "微水镇", "正定县", "正定镇", "栾城县", "栾城镇", "行唐县", "龙州镇", "灵寿县", "灵寿镇", "高邑县", "高邑镇", "深泽县", "深泽镇", "赞皇县", "赞皇镇", "无极县", "无极镇", "平山县", "平山镇", "元氏县", "槐阳镇", "赵县", "赵州镇"]
    }, {
        name: "张家口市",
        districtAndCounty: ["桥西区", "桥东区", "宣化区", "下花园区", "宣化县", "张家口市宣化区", "张北县", "张北镇", "康保县", "康保镇", "沽源县", "平定堡镇", "尚义县", "南壕堑镇", "蔚县", "蔚州镇", "阳原县", "西城镇", "怀安县", "柴沟堡镇", "万全县", "孔家庄镇", "怀来县", "沙城镇", "涿鹿县", "涿鹿镇", "赤城县", "赤城镇", "崇礼县", "西湾子镇"]
    }, {
        name: "承德市",
        districtAndCounty: ["双桥区", "双滦区", "鹰手营子矿区", "承德县", "下板城镇", "兴隆县", "兴隆镇", "平泉县", "平泉镇", "滦平县", "滦平镇", "隆化县", "隆化镇", "丰宁满族自治县", "大阁镇", "宽城满族自治县", "宽城镇", "围场满族蒙古族自治县", "围场镇"]
    }, {
        name: "秦皇岛市",
        districtAndCounty: ["海港区", "山海关区", "北戴河区", "昌黎县", "昌黎镇", "抚宁县", "抚宁镇", "卢龙县", "卢龙镇", "青龙满族自治县", "青龙镇"]
    }]

}];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值