省市县联动的实现。

1、在html中引入jquery.js文件和address.js文件(我的下载中有),写入如下代码。

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        select{
            width:200px;
        }
    </style>
    <script src="./address.js"></script>
    <script src="./jquery-1.8.3.min.js"></script>
</head>
<body>

<div>
    <label>省份</label><select class="provice"></select>
</div>
<div>
    <label>市区</label><select class="city"></select>
</div>
<div>
    <label></label><select class="distance"></select>
</div>
<script>
    var provice=document.getElementsByClassName('provice');
    var city=document.getElementsByClassName('city');
    var distance=document.getElementsByClassName('distance');
    window.onload = function(){
        console.info('2344');
        setAreaSelect($(provice),$(city),$(distance));
    };

    //省市县三级联动下拉框
    function setAreaSelect(provinceSelect, citySelect, countySelect){
        var provDef = '<option value="">请选择省份</option>',
                cityDef = '<option value="">请选择市区</option>',
                countyDef = '<option value="">请选择区县</option>';
        var addrJson = addr;
        var getOpt = function (pId){
            var optStr = "";
            for (var i = 0, addrJsonLen = addrJson.length; i < addrJsonLen; i++){
                var iData = addrJson[i];
                optStr += iData.parentid == pId ? '<option value="' + iData.addrid + '">' + iData.name + '</option>' : '';
            }
            return optStr;
        };
        var getProvince = function (){return  provDef + getOpt("1")};
        var getCity = function (proId){return  cityDef + getOpt(proId)};
        var getCounty = function (cityId){return countyDef + getOpt(cityId)};

        provinceSelect.html(getProvince());
        provinceSelect.on('change' ,function (){
            citySelect.html(cityDef);
            var _pId = $(this).val();
            _pId != "" ? citySelect.html(getCity(_pId)) : '';
            citySelect.change();
        });
        citySelect.on('change' ,function (){
            countySelect.html(countyDef);
            var _cId = $(this).val();
            _cId != "" ? countySelect.html(getCounty(_cId)) : '';
            countySelect.change();
        });
    }

</script>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值