从下拉框中获取文本 [英] Getting the text from a drop-down box
本文介绍了从下拉框中获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这将获得我的下拉菜单中选择的值。
This gets the value of whatever is selected in my dropdown menu.
document.getElementById('newSkill').value
然而,我无法找到下拉菜单中当前显示的文字的什么属性。我试过文字,然后查看 W3Schools ,但没有答案,这里有人知道吗?
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?
对于那些不确定的人,下面是一个下拉框的HTML。
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>
推荐答案
根据您的示例HTML代码,这里有一种方法获取当前所选选项的显示文本:
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屋!
查看全文