DOM之城市二级联动

1、HTML内容

<select id="province">
        <option>请选择</option>
        <option>山东省</option>
        <option>吉林省</option>
        <option>上海市</option>
    </select>
    <select id="city">
        <option>请选择</option>
    </select>

2、JS

var provinceE=document.getElementById("province");
    provinceE.onchange=function(){
        var city=document.getElementById("city");
        var opts=city.getElementsByTagName("option");
        for(var z=opts.length-1;z>0;z--){
            city.removeChild(opts[z]);
        }
        var province=provinceE.value;
        //alert(province);
        var citys=[];
        switch (province){
            case "山东省":
                citys=["青岛市","济南市","威海市","日照市","德州市"];
                break;
            case "吉林省":
                citys=["长春市","四平市","辽源市","通化市","白山市"];
                break;
            case "上海市":
                citys=["嘉定区","普陀区","黄浦区","虹口区","长宁区"];
                break;
        }
        for(var i=0;i<citys.length;i++){
            var option=document.createElement("option");
            var textNode=document.createTextNode(citys[i]);
            option.appendChild(textNode);
            city.appendChild(option);
        }

    }

 

posted @ 2017-02-19 20:09  逗伴不是瓣  阅读(647)  评论(0编辑  收藏  举报