<select id="selTest">
<option value="1">Test</option>
<option value="2"> Test1</option>
<option value="3"> Test2</option>
</select>
JavaScript Code:
function traverseSelect()
{
var sel = document.getElementById('selTest');
for (i = 0; i < sel.options.length; i++)
{
alert(sel.options[i].text);
}
//get the text of selected option
alert(sel.options[sel.options[sel.selectedIndex].text);
}
遍历Select元素示例
本文介绍了一种使用JavaScript遍历HTML中Select元素的方法,并展示了如何获取Select元素中每个选项的文本内容。此外,还提供了获取当前选中项文本的示例代码。
811

被折叠的 条评论
为什么被折叠?



