在javascript中输入ID号时更改选择值选项

mqkwyuun  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(118)

在javascript中输入SA ID号和更改选择值选项时更改性别
如何在输入身份证号输入字段时自动获取性别选择选项值javascript

qvtsj1bj

qvtsj1bj1#

你指的是这个吗?

const setSelectGender = event => {
  const id = event.srcElement.value
  let gender = "male"
  if(id == 1) { gender = "male" }  
  if(id == 2) { gender = "female" }  

  document.querySelectorAll("#gendersSelect option")
  .forEach(opt => { opt.selected = opt.value == gender ? true : false })
}

document.querySelector("#inputNumber")
.addEventListener("change", setSelectGender)
<form>
  <input type="number" id="inputNumber">
  <select id="gendersSelect">
    <option value="male">Male</option>
    <option value="female">Female</option>
  </select>
</form>

相关问题