2009-01-22 13:17:00
如何用javascript改变select的options数组楼主wangtaoyy(flow) 2002-03-11 19:58:41 在 Web 开发 / JavaScript 提问
也就是改变可选项 问题点数:100、回复次数:7Top
1 楼GDXY(宣云) 回复于 2002-03-11 20:47:42 得分 20
设select的id为selectid
用selectid.options[i].text来访问/修改各个选项的文字;
用selectid.options[i].value来访问/修改各个选项对应的值。
其中的i是下标。Top
2 楼westfly(左眼) 回复于 2002-03-11 21:37:18 得分 20
同意gdxy
代码如下:
<select id=slt1>
<option>option1
<option>option2
</select>
<button onclick=ChangeOptionText()>change</button>
<script language=javascript>
function ChangeOptionText()
{
slt1.options[0].innerText=prompt('input first option caption:',slt1.options[0].text);
}
</script>Top
3 楼beyond_xiruo(CorruptionException) 回复于 2002-03-11 22:59:38 得分 30
<body onload="a(document.all.sel1.value);c(document.all.sel1.value,document.all.sel2.value)">
<center>
<select name=sel1 size=1 onchange="javascript:a(this.value);">
<option value="" selected>year
<script>
<!--
for(i=2001;i<=2050;i++)
{
document.write("<option value="+i+">"+i);
}
//-->
</script>
</select>
<select name=sel2 size=1 onchange="javascript:c(document.all.sel1.value,this.value);">
<option value="" selected>month
</select>
<select name=sel3 size=1>
<option value="" selected>date
</select>
</center>
<script>
<!--
function a(b)
{
var year=b;
for(i=31;i>=1;i--)
{
document.all.sel3.options.remove(i);
}
switch(b)
{
case "":
for(i=12;i>=1;i--)
{
document.all.sel2.options.remove(i);
}
break;
default:
for(i=1;i<=12;i++)
{
if(i<10) {i="0"+i}
document.all.sel2.options[i]=new Option(i,i);
}
break;
}
}
function c(d,e)
{
var year=d;
var month=e;
switch(e)
{
case "":
for(i=31;i>=1;i--)
{
document.all.sel3.options.remove(i);
}
break;
case "01":
case "03":
case "05":
case "07":
case "08":
case "10":
case "12":
for(j=1;j<=31;j++)
{
if(j<10) {j="0"+j}
document.all.sel3.options[j]=new Option(j,j);
}
break;
case "04":
case "06":
case "09":
case "11":
document.all.sel3.options.remove(31);
for(j=1;j<=30;j++)
{
if(j<10) {j="0"+j}
document.all.sel3.options[j]=new Option(j,j);
}
break;
case "02":
document.all.sel3.options.remove(31);
document.all.sel3.options.remove(30);
document.all.sel3.options.remove(29);
switch(d%4)
{
case 0:
for(j=1;j<=29;j++)
{
if(j<10) {j="0"+j}
document.all.sel3.options[j]=new Option(j,j);
}
break;
default:
for(j=1;j<=28;j++)
{
if(j<10) {j="0"+j}
document.all.sel3.options[j]=new Option(j,j);
}
break;
}
}
}
//-->
</script>
这是一个选择日期的脚本,自己体会!Top
4 楼8988(晓月) 回复于 2002-03-12 18:13:30 得分 10
你说的“改变”是改变数组中被选择的对象呢,还是改变数组中对象的值,还是增加、删除对象。Top
5 楼tncboy(爆米花) 回复于 2002-03-13 13:44:06 得分 10
收~~!Top
6 楼goldtogo(jinhao) 回复于 2002-03-13 18:39:49 得分 10
给你个例子吧!!!
<script language="javascript">
<!--
function InsertSle(val1,val2,sle)
{
var newsle=new Option(val1,val2);
sle.add(newsle);
}
function DelSle(sle,indexsle)
{
sle.options[indexsle]=null;
}
function DelAndIn(sle1,sle2)
{
for(i=0;i<sle1.length;i++)
if(sle1.options[i].selected){
InsertSle(sle1.options[i].text,sle1.options[i].value,sle2);
DelSle(sle1,i);
}
}
function MoveAll(sle1,sle2)
{
var i=sle1.length-1;
for(;i>=0;i--)
{
InsertSle(sle1.options[i].text,sle1.options[i].value,sle2);
DelSle(sle1,i);
}
}Top
7 楼Reve(仨仁仕) 回复于 2002-03-13 21:24:06 得分 0
楼上说的对。