登录

js实现省市二级联动,三级联动,省份城市二级联动选择核心代码

我来纠错
<select name="Capital" id="Capital" onChange="getCity()">
    <option value="null" selected="selected">请选择...</option>
    <option value="北京">北京</option>
    <option value="上海">上海</option>
    <option  value="天津">天津</option>
  </select>
<select name="City" id="City">
	<option value="请选择城市">请选择城市</option>
</select>

<script>
var city=[
          ["东城","西城","崇文","宣武","朝阳","丰台","石景山","海淀","门头沟","房山","通州","顺义","昌平","大兴","平谷","怀柔","密云","延庆"],
         ["黄浦","卢湾","徐汇","长宁","静安","普陀","闸北","虹口","杨浦","闵行","宝山","嘉定","浦东","金山","松江","青浦","南汇","奉贤","崇明"],
         ["和平","东丽","河东","西青","河西","津南","南开","北辰","河北","武清","红挢","塘沽","汉沽","大港","宁河","静海","宝坻","蓟县"],
          ];
function getCity(){
    //获得省份下拉框的对象
    var sltProvince= document.getElementById('Capital');
    //获得城市下拉框的对象
    var sltCity= document.getElementById('City');
    
    //得到对应省份的城市数组
    var provinceCity=city[sltProvince.selectedIndex - 1];

    //清空城市下拉框,仅留提示选项
    sltCity.length=1;
    //将城市数组中的值填充到城市下拉框中
    for(var i=0;i<provinceCity.length;i++){
        sltCity[i+1] = new Option(provinceCity[i],provinceCity[i]);
    }
}
</script>
发送
热门关键词:
命令
知识类型:
标题描述:
详细解答:

提交审核您编辑的知识会经过 前端大牛 人工审核。