所以我想在我的表单中添加一个下拉列表,它将显示从 1 到 999 的数字。 我尝试过使用 JS 来实现,但没有成功 这是我到目前为止所尝试过的:
<html>
<head>
<script>
window.onload = fillDropDown();
function fillDropDown() {
var ddl = document.getElementById( 'myDropdown' );
var theOption = new Option;
var x;
var i;
for(i = 0; i < 999; i++) {
x = i + 1;
theOption.text = x;
theOption.value = x;
ddl.options[i] = theOption;
}
}
</script>
</head>
<body>
<form id="myForm">
<select id="myDropdown"></select>
</form>
</body>
但是这不起作用。 这是一个 jsfiddle 示例 http://jsfiddle.net/j3nLxptn/
最佳答案
如果您在循环内实例化该选项,它就会起作用:
for (i = 0; i < 999; i++) {
var theOption = new Option;
x = i + 1;
theOption.text = x;
theOption.value = x;
ddl.options[i] = theOption;
}
关于javascript - 使用 JavaScript 填充 <select>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25477389/