当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 原生JS实现省市区(县)三级联动下拉列表的详细教程

原生JS实现省市区(县)三级联动下拉列表的详细教程

2018年10月25日  | 移动技术网IT编程  | 我要评论

接到了公司的新需求,其中有涉及到要选择区域的三级联动下拉列表有关的东西。然后我到网上找有关这方面的资料,发现大多是用插件写的,但是我不想让代码变得很庞大,所以想用原生的js来实现。实现的思路与主要代码如下:

主要的html代码:

 <fieldset>
    <legend>下拉选择框实现省市区(县)三级联动</legend>
    <form action="#">
      <label for="addr-show">您选择的是:
        <input type="text" value="" id="addr-show">
      </label>
      <br>

      <!-- 省份选择 -->
      <select id="prov" onchange="showcity(this)">
        <option>=请选择省份=</option>
      </select>

      <!--城市选择-->
      <select id="city" onchange="showcountry(this)">
        <option>=请选择城市=</option>
      </select>

      <!--县区选择-->
      <select id="country" onchange="seleccountry(this)">
        <option>=请选择县区=</option>
      </select>

      <button type="button" class="btn met1" onclick="showaddr()">确定</button>
    </form>
  </fieldset>

css代码如下:

* {
  margin: 0;
  padding: 0;
}

fieldset {
  width: 500px;
  padding: 20px;
  margin: 30px;
  border: 1px solid #ccc;
}

legend{
  font-size: 18px;
  font-weight: bold;
}

#addr-show {
  width: 200px;
  height: 25px;
  margin-bottom: 10px;
}

.btn {
  width: 80px;
  height: 30px;
  border-radius: 4px;
  border: 1px solid #ccc;
  outline: none;
  background-color: #aaa;
  margin: 0 20px;
}

.btn:disabled{
  background-color:#ccc;
}

select {
  width: 120px;
  height: 30px;
}

select#city {
  display: none;
}

select#country {
  display: none;
}

页面加载时,动态获取省份列表并放到下拉菜单的下拉项中:

/*自动加载省份列表*/
(function showprov() {
  btn.disabled = true;
  var len = provice.length;
  for (var i = 0; i < len; i++) {
    var provopt = document.createelement('option');
    provopt.innertext = provice[i]['name'];
    provopt.value = i;
    prov.appendchild(provopt);
  }
})();

这是一个立即执行函数。
当点击省份的下拉列表时会触发select的onchange事件,我们用options的selectedindex属性找到用户选择的省份,动态的生成相应省得城市列表。

/*根据所选的省份来显示城市列表*/
function showcity(obj) {
  var val = obj.options[obj.selectedindex].value;
  if (val >=0) {
    city.style.display = 'inline-block';
    country.style.display = 'none';
  } else {
    city.style.display = 'none';
    country.style.display = 'none';
  }
  if (val != current.prov) {
    current.prov = val;
    addrshow.value = '';
    btn.disabled = true;
  }
  if (val != null) {
    city.length = 1;
    if (provice[val]) {
      var citylen = provice[val]["city"].length;
    }
    // var citylen = provice[val]["city"].length;
    for (var j = 0; j < citylen; j++) {
      var cityopt = document.createelement('option');
      cityopt.innertext = provice[val]["city"][j].name;
      cityopt.value = j;
      city.appendchild(cityopt);
    }
  }
}

当点击城市列表中的某一项时,原理同上(此处不赘述)

/*根据所选的城市来显示县区列表*/
function showcountry(obj) {
  var val = obj.options[obj.selectedindex].value;
  if (val >=0) {
    country.style.display = 'inline-block';
  } else {
    country.style.display = 'none';
  }
  current.city = val;
  if (val != null) {
    country.length = 1; //清空之前的内容只留第一个默认选项
    if (provice[current.prov]["city"][val]) {
      var countrylen = provice[current.prov]["city"][val].districtandcounty.length;
    }
    // var countrylen = provice[current.prov]["city"][val].districtandcounty.length;
    if(countrylen == 0){
      addrshow.value = provice[current.prov].name + '-' + provice[current.prov]["city"][current.city].name;
      return;
    }
    for (var n = 0; n < countrylen; n++) {
      var countryopt = document.createelement('option');
      countryopt.innertext = provice[current.prov]["city"][val].districtandcounty[n];
      countryopt.value = n;
      country.appendchild(countryopt);
    }
  }
}

我们这里用的省市区数据来自网络,不保证完整性,仅作案例使用。数据格式如下:

var provice = [
  {
    name: "北京市",
    city: [
      {
        name: "北京市",
        districtandcounty: ["东城区", "西城区", "崇文区", "宣武区", "朝阳区", "丰台区", "石景山区", "海淀区", "门头沟区", "房山区", "通州区", "顺义区", "昌平区", "大兴区", "怀柔区", "平谷区", "密云县", "延庆县", "延庆镇"]
      }
    ]
  },
  ...
  {
    name: "澳门",
    city: [
      {
        name: "澳门特别行政区",
        districtandcounty: ["澳门特别行政区"]
      }
    ]
  }

完整版在这里全部省市区(县)数据

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网