在javascript中输入SA ID号和更改选择值选项时更改性别如何在输入身份证号输入字段时自动获取性别选择选项值javascript
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>
1条答案
按热度按时间qvtsj1bj1#
你指的是这个吗?