站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Javascript 专题不刷新页面动态更新select选项,实现两个select相互操作

不刷新页面动态更新select选项,实现两个select相互操作

摘要: 不刷新页面动态更新select选项,实现两个select相互操作


在网上很多网友问,关于实现在一个select列表中选择一个项目,在另一个select选项动态更新的情况。其实他的原理很简单的,只有记住几个主要的要点就可以。

下面的例子给出了一个完整的演示。由于看例子比我解说更容易理解,所以我就废话少说,把代码贴出,希望对大家需要的网友有一点帮助。


<head>
New Document





<script LANGUAGE="JavaScript">

function change()
{
selIndex=document.all("test").selectedIndex;
if(document.all("test").selectedIndex==0)
return;

for(i=document.all("test").options.length-1;i>-1;i--)
{
document.all("test").options.remove(i);
}

for(i=0;i{
document.all("test").options.add(new Option(Name[selIndex][i],"name"+i));
}

}

function changeEx(){


for(i=document.all("sub").options.length;i>0;i--)
{
document.all("sub").options.remove(i-1);
}


if(document.all("main").selectedIndex==0){
document.all("sub").options.add(new Option("==========","-1"));
return;}


selIndex=document.all("main").selectedIndex;

for(i=0;i{
document.all("sub").options.add(new Option(Name[selIndex][i],"name"+i));
}

}

function reset(){
for(i=document.all("test").options.length-1;i>-1;i--)
{
document.all("test").options.remove(i);
}

document.all("test").options.add(new Option("==========","-1"));

document.all("test").options.add(new Option("Zosatapo","1"));

document.all("test").options.add(new Option("Reic Yang","2"));

}

function display(object){
alert(object.options[object.selectedIndex].text+" "+object.options[object.selectedIndex].value);
}
//
</script>
</head>

<body BGCOLOR="#FFFFFF">
First Sample:
All items will change After you Selected!

<select id="test" onchange="change();">
<option value="-1" selected>==========
<option value="1">Zosatapo
<option value="2">Reic Yang
</select><input name="Reset Select" type="button" value="Reset Select" onclick="reset();" >

Second Sample:
You selected Item in Main Select will change the Sub select Content!

Main Select:<select id="main" onchange="changeEx();">
<option value="-1" selected>==========
<option value="1">Zosatapo
<option value="2">Reic Yang
</select>

Sub Select:<select id="sub" onchange="display(this);">
<option value="-1" selected>==========
</select>


</body>

</td> </tr> <tr> <td vAlign=top align=left height="100%">
↑返回目录
前一篇: javascript/Jscript实现父子窗体的互相引用问题(Powered By ZosaTapo)
后一篇: 网页输入框日期型有效性判定一网打尽