如何使用javascript获取Select的显示值 [英] How to get the display value of Select using javascript

查看:35
本文介绍了如何使用javascript获取Select的显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Select>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</Select>

我正在使用 document.getElementById(Example)。value; 获取值。

我想显示文本而不是值。例如。 value = 1 - >一个。如何获得一个文本?

I want to display the text instead of the value. eg. value=1 --> One. How can I get the One text?

推荐答案

在纯JavaScript中你可以这样做:

In plain JavaScript you can do this:

<select id="Example">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>





var sel = document.getElementById("Example");
var value = sel.options[sel.selectedIndex].value; // or just sel.value
var text = sel.options[sel.selectedIndex].text; 

jQuery:

var $sel = $("#Example");
var value = $sel.val();
var text = $("option:selected",$sel).text(); 

这篇关于如何使用javascript获取Select的显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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