如何使用JavaScript在下拉列表中显示所选选项的索引?
要在下拉列表中显示所选选项的索引,请使用JavaScript中的selectedIndex属性。
示例
您可以尝试运行以下代码以显示所选选项的索引-
<!DOCTYPE html>
<html>
<body>
<form id="myForm">
<select id="selectNow">
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>
<input type="button" onclick="display()" value="Click">
</form>
<p>Select and click the button to get the index of the selected option.</p>
<script>
function display() {
var index = document.getElementById("selectNow").selectedIndex;
document.write(index);
}
</script>
</body>
</html>热门推荐
10 预祝高尔夫的祝福语简短
11 中式婚礼贺词祝福语简短
12 老同学十一祝福语简短
13 生日寿宴祝福语简短精辟
14 鼓励孩子小考祝福语简短
15 鼠年新年贺卡祝福语简短
16 生日祝福语男兄弟简短
17 请灵回向祝福语简短
18 新娘对伴娘祝福语简短