如何使用 JavaScript 显示下拉列表中的所有选项?
要显示下拉列表中的所有选项,请使用 options 属性。此属性允许你使用 length 属性获取所有选项。
示例
你可以尝试运行以下代码以获取下拉列表中的所有选项。
<!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>Click the button to get all the options</p>
<script>
function display() {
var a, i, options;
a = document.getElementById("selectNow");
options = "";
for (i = 0; i < a.length; i++) {
options = options + "<br> " + a.options[i].text;
}
document.write("DropDown Options: "+options);
}
</script>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
JavaScript
PHP