<script language="JavaScript">
<!--
function TwoSelectInit(so,dv1,dv2){
var o1=so.o1;var o2=so.o2;var allstr=so.str;var dt1=so.dt1;var dt2=so.dt2;var selectonce=so.selectonce;
var _s = "*|^@";
var s1=new Array(),v1=new Array(),s2=new Array(),v2=new Array();
var s1i = 0,s2i = 0;
if(dt1!=""){
if(!selectonce){allstr=dt1+_s.charAt(1)+_s.charAt(0)+allstr;}
else{allstr=dt1+_s.charAt(1)+dt2+_s.charAt(0)+allstr;}
}
aa=allstr.split(_s.charAt(0));
for(aai=0;aai<aa.length;aai++){
aaa=aa[aai].split(_s.charAt(1));
tmps1 = aaa[0].split(_s.charAt(3));
s1[aai] = tmps1[0];v1[aai] = (tmps1.length==2)?tmps1[1]:tmps1[0];
s2[aai] = new Array();v2[aai] = new Array();
if(v1[aai]==dv1){s1i = aai;}
bbbb=aaa[1];
if(dt2!=""&&!selectonce){if(bbbb==""){bbbb=dt2;}else{bbbb=dt2+_s.charAt(2)+bbbb;}}
bb=bbbb.split(_s.charAt(2));
for(bbi=0;bbi< bb.length;bbi++){
tmps2 = bb[bbi].split(_s.charAt(3));
s2[aai][bbi] = tmps2[0];v2[aai][bbi] = (tmps2.length==2)?tmps2[1]:tmps2[0];
if(v2[aai][bbi]==dv2){s2i = bbi;}
}
}
for(var i=0;i<o1.options.length;i++){o1.remove(i);i--;}
for(k=0;k<s1.length;k++){o1.options.add(new Option(s1[k],v1[k]));}
o1.selectedIndex=s1i;
for(var i=0;i<o2.options.length;i++){o2.remove(i);i--;}
for(k=0;k<s2[s1i].length;k++){o2.options.add(new Option(s2[s1i][k],v2[s1i][k]));}
o2.selectedIndex=s2i;
}
function HwTwoSelect(o1,o2,liststr,dt1,dt2,t){this.o1=o1;this.o2=o2;this.str=liststr;this.dt1=dt1;this.dt2=dt2;this.selectonce=t}
//-->
</SCRIPT>
<form method="post" name=myform>
选择一次:<select name="a" onchange="TwoSelectInit(test1,this.value)"></select> <select name="b"></select><hr>
选择两次:<select name="aa" onchange="TwoSelectInit(test2,this.value)"></select> <select name="bb"></select><hr>
有初始值:<select name="aaa" onchange="TwoSelectInit(test3,this.value)"></select> <select name="bbb"></select>
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
var selecttext=""
+"搜索@search|Google@http://www.google.com/^Yahoo@http://www.yahoo.com/"
+"*收藏@fav|无忧视窗@http://www.51windows.Net/^蓝色理想@http://www.blueidea.com/^POPO@http://plod.popoever.net/"
+"";
var test1 = new HwTwoSelect(document.myform.a,document.myform.b,selecttext,"-分类-@","-网址-@",1);//最后的参数表示,选了一级菜单后,二级菜单是否还要显示“请选择”,1或true代码,不显示,0或false代表显示
TwoSelectInit(test1);
var test2 = new HwTwoSelect(document.myform.aa,document.myform.bb,selecttext,"-分类-@","-网址-@",0);
TwoSelectInit(test2);
var test3 = new HwTwoSelect(document.myform.aaa,document.myform.bbb,selecttext,"-分类-@","-网址-@",0);
TwoSelectInit(test3,"fav","http://www.51windows.Net/");
//-->
</SCRIPT>
以下是示例代码,详细请查看源文件:
<form method="post" name=myform>
<select name="select1" onchange="TwoSelectInit(ttt,this.value)"></select> <select name="select2"></select><hr>
</form>
<SCRIPT LANGUAGE="JavaScript">
<!--
var selecttext=""
+"搜索@search|Google@http://www.google.com/^Yahoo@http://www.yahoo.com/"
+"*收藏@fav|无忧视窗@http://www.51windows.Net/^蓝色理想@http://www.blueidea.com/^POPO@http://plod.popoever.net/"
+"";
var ttt = new HwTwoSelect(document.myform.select1,document.myform.select2,selecttext,"-分类-@","-网址-@",1);
TwoSelectInit(ttt ,"fav","http://www.51windows.Net/");
//-->
</SCRIPT>
bug:在二级菜单有初始值时,如果表单重置,二级菜单的值会变为空
posted on 2007-10-19 11:06
I LOVE JAVA 阅读(646)
评论(0) 编辑 收藏 所属分类:
JavaScript