- <html>
- <body>
- <div id="Box"></div>
- <a onclick="Show()" href="#">点击取值</a>
- </body>
- </html>
- <script>
- /*
- 名称:联动框组件
- 作者:王旭
- qq:316729240
- 邮箱:wangxu0929@yeah.net
- */
- Object.prototype.attachEvent=function(method,func)
- {
- if(!this[method])
- this[method]=func;
- else
- this[method]=this[method].attach(func);
- }
- Function.prototype.attach=function(func){
- var f=this;
- return function(){
- f();
- func();
- }
- }
- function ASelectBox(D)
- {
- var T = new Object;
- T.L=new Array();//存储当前下拉控件
- T.RootID=null;
- T.Index=0;
- T.DataID=new Array();
- T.ParentID=new Array();
- T.Text=new Array();
- T.Count=0;
- T.Add=function(parentid,id,text)
- {
- T.DataID[T.Index]=id;
- T.ParentID[T.Index]=parentid;
- T.Text[T.Index]=text;
- T.Index++;
- }
- T.Create=function(M,I)
- {
- for(var i=I;i<T.L.length;i++)
- {
- T.L[i].removeNode(true);
- }
- if(I!=null)T.L=(T.L.slice(0,I))
- if(I==null)I=0;
- if(M==null)M=T.RootID;
- var C2=document.createElement('select');
- for(var n=0;n<T.Index;n++)
- {
- if(T.ParentID[n]==M)
- {
- C2.options.add(new Option(T.Text[n],T.DataID[n]))
- }
- }
- if(C2.length>0)
- {
- D.appendChild(C2);
- C2.attachEvent("onchange",function(){T.Create(C2.value,I+1);})
- T.L[I]=C2;
- T.Create(C2.value,I+1)
- }
- }
- return(T);
- }
- //调用--------------------------
- var T=ASelectBox(Box)
- T.RootID=1;
- T.Add(1,11,"中国")
- T.Add(1,12,"美国")
- T.Add(1,13,"日本")
- T.Add(11,111,"北京")
- T.Add(11,121,"上海")
- T.Add(12,131,"本土")
- T.Add(111,151,"上海1")
- T.Create();
- //-----------------------------
- function Show()
- {
- alert("共"+T.L.length+"选择框")
- for(var n=0;n<T.L.length;n++)
- {
- alert("第"+(n+1)+"个:"+T.L[n].value)
- }
- }
- </script>
封装的多级联动框
最新推荐文章于 2022-07-13 10:50:40 发布