在javascript中输入ID Number时更改select value选项



在javascript中输入SA ID号并更改select value选项时更改性别

如何在输入ID号输入字段时自动获得性别选择选项值javascript

<body>
<form id="form">
<input type="number" value="5" id="inputNumber">
<select id="gendersSelect">
<option value="male" >Male</option>
<option value="female">Female</option>
</select>
</form>
<script>
function setGender(event){
let gen=this.value>5?'male':'female';
document.getElementById("gendersSelect").value=gen;
}
document.getElementById("inputNumber").addEventListener('keyup',setGender);       
</script>
</body>

你是这个意思吗?

const setSelectGender = event => {
const id = document.querySelector("#inputNumber").value
document.querySelectorAll("#gendersSelect option").forEach(opt => { opt.selected = opt.value == getSexByID(id) ? true : false })
}
document.querySelector("#inputNumber")
.addEventListener("change", setSelectGender)
window.addEventListener("keyup", setSelectGender)
const getSexByID = id => {
strlenth = (id.length-1);
for (var i=0;i<strlenth;i++){
if (i == 6){
return (id[i] > 4) ? 'male' : 'female'
}
}
}
<form id="form">
<input type="number" value="5" id="inputNumber">
<select id="gendersSelect">
<option value="male" >Male</option>
<option value="female">Female</option>
</select>
</form>

相关内容

  • 没有找到相关文章

最新更新