充分利用下拉框中的文本 [英] Getting the text from a drop-down box

查看:149
本文介绍了充分利用下拉框中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这获得无论是在我的下拉菜单中选择的值。

 的document.getElementById('newSkill')。值

我却无法找到后去什么属性,因此目前的下拉菜单中显示的文本。我试图文本然后看着 W3Schools的但这并没有答案,是否有人知道这里?

对于那些不知道,这里是一个下拉框的HTML。

 <选择名称=newSkillID =newSkill>
    <期权价值=1>将技能​​与LT; /选项>
    <期权价值=2>另一个技能< /选项>
    <期权价值=3>另一个技能< /选项>
< /选择>


解决方案

根据你的榜样,HTML code,这里有一个方法来获取当前选定的选项的显示的文本:

  VAR skillsSelect =的document.getElementById(newSkill);
VAR selectedText = skillsSelect.options [skillsSelect.selectedIndex]的.text;

This gets the value of whatever is selected in my dropdown menu.

document.getElementById('newSkill').value

I cannot however find out what property to go after for the text that's currently displayed by the drop down menu. I tried "text" then looked at W3Schools but that didn't have the answer, does anybody here know?

For those not sure, here's the HTML for a drop down box.

<select name="newSkill" id="newSkill">
    <option value="1">A skill</option>
    <option value="2">Another skill</option>
    <option value="3">Yet another skill</option>
</select>

解决方案

Based on your example HTML code, here's one way to get the displayed text of the currently selected option:

var skillsSelect = document.getElementById("newSkill");
var selectedText = skillsSelect.options[skillsSelect.selectedIndex].text;

这篇关于充分利用下拉框中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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