英文:
I don't know how to retrieve the currently selected value in a field
问题
如何获取下拉菜单中当前选定的数值?我不知道该如何做,我尝试了多种方法。
英文:
does anyone know how to get the currently selected value from the select field? I have no idea how to do it, I tried many ways?
答案1
得分: 1
document.getElementById('myButton').addEventListener('click', function () {
console.log(document.getElementById('mySelect').value)
})
<select id="mySelect">
<option value="a">a</option>
<option value="b">b</option>
</select>
<button id="myButton" type="button">Show Selection</button>
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.getElementById('myButton').addEventListener('click', function () {
console.log(document.getElementById('mySelect').value)
})
<!-- language: lang-html -->
<select id="mySelect">
<option value="a">a</option>
<option value="b">b</option>
</select>
<button id="myButton" type="button">Show Selection</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论