从“更改时选择”中获取选定的值/文本
<select onchange="test()" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
</select>
我需要在 javascript 中获取所选选项的值:有谁知道如何获取所选值或文本,请告诉我如何为其编写函数。我已经分配了 onchange() 函数来选择,那么之后我该怎么办?
<select onchange="test()" id="select_id">
<option value="0">-Select-</option>
<option value="1">Communication</option>
</select>
I need to get the value of the selected option in javascript: does anyone know how to get the selected value or text, please tell how to write a function for it. I have assigned onchange() function to select so what do i do after that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
为此,请使用 JavaScript 或 jQuery。
使用 JavaScript
使用 jQuery
Use either JavaScript or jQuery for this.
Using JavaScript
Using jQuery
如果您正在谷歌搜索此内容,并且不希望事件侦听器成为属性,请使用:
If you're googling this, and don't want the event listener to be an attribute, use:
哇,答案中还没有真正可重用的解决方案..我的意思是,标准事件处理程序应该只获得一个
event
参数,而根本不必使用 ids..我会使用:你可以将此处理程序与每个 - 内联 js:
jQuery:
或 vanilla JS 处理程序设置:
一起使用,并且不必为您拥有的每个
select
元素重写此处理程序。示例片段:
Wow, no really reusable solutions among answers yet.. I mean, a standard event handler should get only an
event
argument and doesn't have to use ids at all.. I'd use:You may use this handler with each – inline js:
jQuery:
or vanilla JS handler setting:
And don't have to rewrite this for each
select
element you have.Example snippet:
不需要 onchange 函数。您可以在一行中获取该值:
或者,将其拆分以获得更好的可读性:
No need for an onchange function. You can grab the value in one line:
Or, split it up for better readability:
我想知道每个人都发布了从
获取
value
和text
选项,但没有人建议label
。所以我也建议
label
,所有浏览器都支持获取
value
(与其他人的建议相同)获取
option
text< /code>(即通信或-选择-)
OR(新建议)
HTML
另外
I wonder that everyone has posted about
value
andtext
option to get from<option>
and no one suggestedlabel
.So I am suggesting
label
too, as supported by all browsersTo get
value
(same as others suggested)To get
option
text
(i.e. Communication or -Select-)OR (New suggestion)
HTML
Also
HTML:
JS:
HTML:
JS:
为什么让它过于复杂:
Why overcomplicate it:
使用
Or 来获取值:
Use
Or to get the value:
在警报中,您将看到所选索引的 INT 值,将选择视为数组,您将获得该值
in the alert you'll see the INT value of the selected index, treat the selection as an array and you'll get the value
这是一个老问题,但我不确定为什么人们不建议使用事件对象来检索信息,而不是再次搜索 DOM。
只需浏览函数 onChange 中的事件对象,请参阅下面的示例
function test() {
console.log(event.srcElement.value);
}
http://jsfiddle.net/Corsico/3yvh9wc6/5/
如果这不是 7 年前的默认行为,那么今天查找此内容的人可能会有用
This is an old question, but I am not sure why people didn't suggest using the event object to retrieve the info instead of searching through the DOM again.
Simply go through the event object in your function onChange, see example bellow
function test() {
console.log(event.srcElement.value);
}
http://jsfiddle.net/Corsico/3yvh9wc6/5/
Might be useful to people looking this up today if this wasn't default behavior 7 years ago
在 html 中
在 javascript/typescript 中
In html
In javascript/typescript
您可以通过将“this.value”作为参数传递给名为
test(this.value)
的函数,然后从 select 元素中获取值您应该在脚本元素内创建带有参数的函数,最后您可以在此函数内写入
console.log(number)
来获取您选择的值。You can get the value from the select element by passing "this.value" as a parameter to your function named
test(this.value)
and after thatYou should create the function with a parameter inside the script element and finally you can write
console.log(number)
inside this function to get Your selected value.调用
onChange
后,您可以添加 JS 或 JQuery 代码片段来让您的想法发挥作用。Once the
onChange
is invoked you can add either JS or JQuery Code Snippet to get your thought working.您将 onchange 处理程序设置为某个函数或字符串。在其中,您编写代码来获取值
,或者对于旧版浏览器
You set the onchange handler to some function or string. In it, you write code to get value
or, for older browsers