因此,我想在表单中添加一个下拉列表,以显示从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;
    }


http://jsfiddle.net/j3nLxptn/1/

关于javascript - 使用JavaScript填写<select>,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25477389/

10-13 01:53