封装的多级联动框

来源:互联网 发布:python 距离相关系数 编辑:程序博客网 时间:2024/05/12 03:29
 
  1. <html>
  2. <body>
  3. <div id="Box"></div>
  4. <a onclick="Show()" href="#">点击取值</a>
  5. </body>
  6. </html>
  7. <script>
  8. /*
  9. 名称:联动框组件
  10. 作者:王旭
  11. qq:316729240
  12. 邮箱:wangxu0929@yeah.net
  13. */
  14. Object.prototype.attachEvent=function(method,func)
  15. {
  16.  if(!this[method])
  17.   this[method]=func;
  18.  else
  19.   this[method]=this[method].attach(func);
  20. }
  21. Function.prototype.attach=function(func){
  22.  var f=this;
  23.  return function(){
  24.   f();
  25.   func();
  26.  }
  27. }
  28. function ASelectBox(D)
  29. {
  30.     var T = new Object;
  31.     T.L=new Array();//存储当前下拉控件
  32.     T.RootID=null;
  33.     T.Index=0;
  34.     T.DataID=new Array();
  35.     T.ParentID=new Array();
  36.     T.Text=new Array();
  37.     T.Count=0;
  38.     T.Add=function(parentid,id,text)
  39.     {
  40.         T.DataID[T.Index]=id;
  41.         T.ParentID[T.Index]=parentid;
  42.         T.Text[T.Index]=text;
  43.         T.Index++;
  44.     }
  45.     T.Create=function(M,I)
  46.     {
  47.         for(var i=I;i<T.L.length;i++)
  48.         {
  49.             T.L[i].removeNode(true);
  50.         }
  51.         if(I!=null)T.L=(T.L.slice(0,I))
  52.         if(I==null)I=0;
  53.         if(M==null)M=T.RootID;
  54.         var C2=document.createElement('select');
  55.         for(var n=0;n<T.Index;n++)
  56.         {
  57.             if(T.ParentID[n]==M)
  58.             {
  59.                 C2.options.add(new Option(T.Text[n],T.DataID[n]))
  60.             }
  61.         }
  62.         if(C2.length>0)
  63.         {
  64.             D.appendChild(C2);
  65.             C2.attachEvent("onchange",function(){T.Create(C2.value,I+1);})
  66.             T.L[I]=C2;
  67.             T.Create(C2.value,I+1)
  68.         }
  69.         
  70.     }
  71.     return(T);
  72. }
  73. //调用--------------------------
  74. var T=ASelectBox(Box)
  75. T.RootID=1;
  76. T.Add(1,11,"中国")
  77. T.Add(1,12,"美国")
  78. T.Add(1,13,"日本")
  79. T.Add(11,111,"北京")
  80. T.Add(11,121,"上海")
  81. T.Add(12,131,"本土")
  82. T.Add(111,151,"上海1")
  83. T.Create();
  84. //-----------------------------
  85. function Show()
  86. {
  87. alert("共"+T.L.length+"选择框")
  88. for(var n=0;n<T.L.length;n++)
  89. {
  90.     alert("第"+(n+1)+"个:"+T.L[n].value)
  91. }
  92. }
  93. </script>
原创粉丝点击