如何获取下拉列表中所选值的文本? [英] How to get the text of the selected value of a dropdown list?

查看:72
本文介绍了如何获取下拉列表中所选值的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jQuery获取特定的选项标签文本
如何获取使用jquery的select的select选项的文本?

Possible Duplicate:
jQuery get specific option tag text
How to get the text of the selected option of a select using jquery?

我有一个下拉列表,我想知道所选项目的文本.例如:

I have a dropdown list and I want to know the text of the selected item. For example:

<select>
    <option value="1">Volvo</option>
    <option value="2">Saab</option>
    <option value="3">Mercedes</option>
</select>

如果我知道所选的值,如何获得它的文本值?例如,如果值为1,如何获取Volvo?

If I know the selected value, how can I get it's text value? For instance, if the value is 1 how can I get Volvo?

不胜感激.

推荐答案

您可以使用option:selected来获取select元素的选定选项,然后使用text()方法:

You can use option:selected to get the chosen option of the select element, then the text() method:

$("select option:selected").text();

这是一个例子:

console.log($("select option:selected").text());

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select>
    <option value="1">Volvo</option>
    <option value="2" selected="selected">Saab</option>
    <option value="3">Mercedes</option>
</select>

这篇关于如何获取下拉列表中所选值的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆