js题目11

<!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>
  <style>
  *{
    margin: 0;
    padding: 0;
  }
  select{
    min-width: 100px;
  }
  option{
    display: inline-block;
    min-width: 100px;
  }
  button{
    width: 50px;
    height: 50px;
    margin-top: 50px;
  }
  </style>
  <body>
    <div id="container">
      <select name="" id="sheng">
        <option value="" selected disabled>请选择</option>
      </select>
      <select name="" id="shi">
        <option value="" selected disabled>请选择</option>
      </select>
  
      <select name="" id="qu">
        <option value="" selected disabled>请选择</option>
      </select>
    </div>
    <script>
      const sheng = document.getElementById("sheng")
      const shi = document.getElementById("shi")
      const qu = document.getElementById("qu")
      const arrSheng = ["江苏省", "安徽省"]
      for (let i = 0; i < arrSheng.length; i++) {
        const option = document.createElement("option")
        option.innerHTML = arrSheng[i]
        option.value = arrSheng[i]
        sheng.appendChild(option)
      }
      const objSheng = {
        "江苏省": ["南通市", "扬州市", "南京市"],
        "安徽省": ["合肥市", "淮北市", "宿州市"]
      }
      const objShi = {
        "南通市": ["崇川区", "港闸区"],
        "扬州市": ["邗江区", "广陵区"],
        "南京市": ["玄武区", "秦淮区"],
        "合肥市": ["瑶海区", "庐阳区"],
        "淮北市": ["相山区", "杜集区"],
        "宿州市": ["埇桥区", "萧县"],
      }
      sheng.onchange = function () {
        shi.innerHTML = "<option value=''selected disabled>请选择</option>"
        qu.innerHTML = "<option value=''selected disabled>请选择</option>"
        for (let i = 0; i < Object.keys(objSheng).length; i++) {
          if (sheng.value == Object.keys(objSheng)[i]) {
            const arr = objSheng[Object.keys(objSheng)[i]]
            for (let j = 0; j < arr.length; j++) {
              const option = document.createElement("option")
              option.innerHTML = arr[j]
              option.value = arr[j]
              shi.appendChild(option)
            }
          }
        }
      }
      shi.onchange = function () {
        qu.innerHTML = "<option value=''selected disabled>请选择</option>"
        for (let i = 0; i < Object.keys(objShi).length; i++) {
          if (shi.value == Object.keys(objShi)[i]) {
            const arr = objShi[Object.keys(objShi)[i]]
            for (let j = 0; j < arr.length; j++) {
              console.log(arr[j])
              const option = document.createElement("option")
              option.innerHTML = arr[j]
              option.value = arr[j]
              qu.appendChild(option)
            }
          }
        }
      }
    </script>
  </body>
  </html>

11

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值