大家帮我看看这个函数是哪里出问题了?
function select_list( sel_name, sel_num) { var sel_list = window.document.createElement("select");
sel_list.options.length = sel_num;
sel_list.name = sel_name;
var i = 0;
for(i=0;i<sel_num;i++)
{
var option = document.createElement("option");
option[i].text = i;
option[i].value = i;
sel_list.appendChild(option);
}
}
<table>
<tr>
<td onload ="select_list('channel_index',5)">
</td>
</tr>
</table>
因为做的网页中在很多地方都有下拉列表,为了方便这个网页可以应用于不同的产品需求(下拉参数的个数,和值会不一样),所以就想要写一个函数,来动态生成它。
这几天看了很多资料和例子,写了这个函数,好像没有语法问题,但是还是没有达到我想要的效果。
大家,帮我看一下~谢谢!
function select_list( sel_name, sel_num) { var sel_list = window.document.createElement("select");
sel_list.options.length = sel_num;
sel_list.name = sel_name;
var i = 0;
for(i=0;i<sel_num;i++)
{
var option = document.createElement("option");
option[i].text = i;
option[i].value = i;
sel_list.appendChild(option);
}
}
<table>
<tr>
<td onload ="select_list('channel_index',5)">
</td>
</tr>
</table>
因为做的网页中在很多地方都有下拉列表,为了方便这个网页可以应用于不同的产品需求(下拉参数的个数,和值会不一样),所以就想要写一个函数,来动态生成它。
这几天看了很多资料和例子,写了这个函数,好像没有语法问题,但是还是没有达到我想要的效果。
大家,帮我看一下~谢谢!
function select_list( sel_name, sel_num) { var sel_list = window.document.createElement("select");
sel_list.options.length = 0;
sel_list.name = sel_name;
var i = 0;
for(i=0;i <sel_num;i++)
{
var option = document.createElement("option");
option.innerText = i;
option.value = i;
sel_list.appendChild(option);
}
document.body.appendChild(sel_list);
}
</script>
<body onload ="select_list('channel_index',5)">
</body>
function select_list( sel_name, sel_num) { var sel_list = document.createElement("select");
sel_list.name = sel_name;
for(var i=0;i <sel_num;i++)
{
sel_list.add(new Option(i,i));
}
document.body.appendChild(sel_list);
}
</script>
<body>
<script language="javascript">
<!--
function select_list(sel_name, sel_num,id) {
var sel_list = document.createElement("select");
document.getElementById(id).appendChild(sel_list)
sel_list.options.length = 0;
sel_list.name = sel_name;
var i = 0;
for(i=0;i <sel_num;i++) {
var option = document.createElement("option");
sel_list.appendChild(option);
option.text = i;
option.value = i;
}
}
//-->
</script><table border=1>
<tr>
<td id=s1><script language="javascript">select_list('channel_index',5,'s1')</script>
</td>
</tr>
</table> <table border=1>
<tr>
<td id=s2><script language="javascript">select_list('channel_index',5,'s2')</script>
</td>
</tr>
</table> </body>