检索所选的< option>的文本在< select>元件 [英] Retrieving the text of the selected <option> in <select> element
本文介绍了检索所选的< option>的文本在< select>元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下内容中:
<select id="test">
<option value="1">Test One</option>
<option value="2">Test Two</option>
</select>
如何获取所选选项的文本(即Test One或Test Two )使用JavaScript
How can I get the text of the selected option (i.e. "Test One" or "Test Two") using JavaScript
document.getElementsById('test')。selectedValue
返回1或2,什么属性返回
document.getElementsById('test').selectedValue
returns 1 or 2, what property returns the text of the selected option?
推荐答案
function getSelectedText(elementId) {
var elt = document.getElementById(elementId);
if (elt.selectedIndex == -1)
return null;
return elt.options[elt.selectedIndex].text;
}
var text = getSelectedText('test');
这篇关于检索所选的< option>的文本在< select>元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文