使用原生js实现一个简单的三级联动

1

在这里插入代码片
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <select name="" id="" class="province">
      <option>--请选择城市--</option>
    </select>
    <select name="" id="" class="city">
      <option>--请选择城市--</option>
    </select>
    <select name="" id="" class="county">
      <option>--请选择城市--</option>
    </select>
    <script>
      var data = [
        {
          value: '吉林省1',
          children: [
            {
              value: '通话市1',
              children: [
                {
                  value: 'aa1'
                }
              ]
            },
            {
              value: '白山市1'
            }
          ]
        },
        {
          value: '吉林省2',
          children: [
            {
              value: '通话市2'
            },
            {
              value: '白山市2',
              children: [
                {
                  value: 'bb2'
                }
              ]
            }
          ]
        },
        {
          value: '吉林省3',
          children: [
            {
              value: '通话市3'
            },
            {
              value: '白山市3',
              children: [
                {
                  value: 'cc2'
                }
              ]
            }
          ]
        }
      ]
      var province = document.querySelector('.province')
      var city = document.querySelector('.city')
      var county = document.querySelector('.county')
      // 将省级内容渲染到第一个下拉框中
      data.forEach((val, index) => {
        var option = document.createElement('option')
        option.value = index
        option.innerHTML = val.value
        province.appendChild(option)
      })

      province.onchange = function() {
        // 初始化市 县
        city.innerHTML = '<option>--请选择城市--</option>'
        county.innerHTML = '<option>--请选择城市--</option>'
        // 渲染市的下拉框
        data[this.value] ? data[this.value] : []
        data[this.value].children.forEach((val, index) => {
          var option = document.createElement('option')
          option.value = index
          option.innerHTML = val.value
          city.appendChild(option)
        })
        var that = this
        // 渲染县级下拉框
        city.onchange = function() {
          //  初始化县级的数据
          data[that.value].children[this.value].children =
            data[that.value].children[this.value].children || []
          // 渲染县级数据
          data[that.value].children[this.value].children.forEach(
            (val, index) => {
              var option = document.createElement('option')
              option.value = index
              option.innerHTML = val.value
              county.appendChild(option)
            }
          )
        }
      }
    </script>
  </body>
</html>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值