填写a<;选择>;使用JavaScript
所以我想在我的表单中添加一个下拉列表,显示从1到999的数字。 我试着用JS来做,但是没有用 以下是我迄今为止所尝试的:填写a<;选择>;使用JavaScript,javascript,html,forms,Javascript,Html,Forms,所以我想在我的表单中添加一个下拉列表,显示从1到999的数字。 我试着用JS来做,但是没有用 以下是我迄今为止所尝试的: <html> <head> <script> window.onload = fillDropDown(); function fillDropDown() { var ddl = document.getElementById( 'myDropdown' ); var theOption = new Option; var x;
<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>
window.onload=fillDropDown();
函数fillDropDown(){
var ddl=document.getElementById('myDropdown');
var期权=新期权;
var x;
var i;
对于(i=0;i<999;i++){
x=i+1;
theOption.text=x;
theOption.value=x;
ddl.options[i]=选项;
}
}
但它不起作用。
下面是一个JSFIDLE示例如果您在循环中实例化该选项,它就会起作用:
for (i = 0; i < 999; i++) {
var theOption = new Option;
x = i + 1;
theOption.text = x;
theOption.value = x;
ddl.options[i] = theOption;
}
(i=0;i<999;i++)的{
var期权=新期权;
x=i+1;
theOption.text=x;
theOption.value=x;
ddl.options[i]=选项;
}
请参见如果在循环内实例化该选项,则该选项有效:
for (i = 0; i < 999; i++) {
var theOption = new Option;
x = i + 1;
theOption.text = x;
theOption.value = x;
ddl.options[i] = theOption;
}
(i=0;i<999;i++)的{
var期权=新期权;
x=i+1;
theOption.text=x;
theOption.value=x;
ddl.options[i]=选项;
}
请参见如果在循环内实例化该选项,则该选项有效:
for (i = 0; i < 999; i++) {
var theOption = new Option;
x = i + 1;
theOption.text = x;
theOption.value = x;
ddl.options[i] = theOption;
}
(i=0;i<999;i++)的{
var期权=新期权;
x=i+1;
theOption.text=x;
theOption.value=x;
ddl.options[i]=选项;
}
请参见如果在循环内实例化该选项,则该选项有效:
for (i = 0; i < 999; i++) {
var theOption = new Option;
x = i + 1;
theOption.text = x;
theOption.value = x;
ddl.options[i] = theOption;
}
(i=0;i<999;i++)的{
var期权=新期权;
x=i+1;
theOption.text=x;
theOption.value=x;
ddl.options[i]=选项;
}
请参见问题在于您总是重复使用相同的选项元素。您必须在每次迭代时创建一个新实例,并使用它来实际附加select选项 还要注意,
选项
构造函数可以获取文本
和值
参数
for (i = 0; i < 999; i++) {
x = i + 1;
ddl.add(new Option(x, x), null);
}
(i=0;i<999;i++)的{
x=i+1;
ddl.add(新选项(x,x),null);
}
问题在于您总是重复使用相同的选项元素。您必须在每次迭代时创建一个新实例,并使用它来实际附加select选项
还要注意,选项
构造函数可以获取文本
和值
参数
for (i = 0; i < 999; i++) {
x = i + 1;
ddl.add(new Option(x, x), null);
}
(i=0;i<999;i++)的{
x=i+1;
ddl.add(新选项(x,x),null);
}
问题在于您总是重复使用相同的选项元素。您必须在每次迭代时创建一个新实例,并使用它来实际附加select选项
还要注意,选项
构造函数可以获取文本
和值
参数
for (i = 0; i < 999; i++) {
x = i + 1;
ddl.add(new Option(x, x), null);
}
(i=0;i<999;i++)的{
x=i+1;
ddl.add(新选项(x,x),null);
}
问题在于您总是重复使用相同的选项元素。您必须在每次迭代时创建一个新实例,并使用它来实际附加select选项
还要注意,选项
构造函数可以获取文本
和值
参数
for (i = 0; i < 999; i++) {
x = i + 1;
ddl.add(new Option(x, x), null);
}
(i=0;i<999;i++)的{
x=i+1;
ddl.add(新选项(x,x),null);
}