我不知道如何检索字段中当前选择的值。

huangapple go评论56阅读模式
英文:

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(&#39;myButton&#39;).addEventListener(&#39;click&#39;, function () {
  console.log(document.getElementById(&#39;mySelect&#39;).value)
})

<!-- language: lang-html -->

&lt;select id=&quot;mySelect&quot;&gt;
  &lt;option value=&quot;a&quot;&gt;a&lt;/option&gt;
  &lt;option value=&quot;b&quot;&gt;b&lt;/option&gt;
&lt;/select&gt;

&lt;button id=&quot;myButton&quot; type=&quot;button&quot;&gt;Show Selection&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月27日 03:18:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定