前面我讲了普通的无限联动下拉框,但是,如果要指定select呢?这就要用下面这个联动下拉框类了。
  由于要指定select,所以,前面的容器不需要了,我们用个数组把指定的select保存,另外,所用数据的结构稍有变化,多了个深度项,无限分类中每项肯定是有个深度的。
  所用数据sortArr.js如下:
var arrSorts = new Array(35);
arrSorts[0] = ["1", "主类别一", "0", "1"];
arrSorts[1] = ["2", "主类别二", "0", "1"];
arrSorts[2] = ["3", "主类别三", "0", "1"];
arrSorts[3] = ["4", "小类一", "1", "2"];
arrSorts[4] = ["5", "小类二", "1", "2"];
arrSorts[5] = ["6", "小类三", "1", "2"];
arrSorts[6] = ["7", "细类一", "4", "3"];
arrSorts[7] = ["8", "细类二", "4", "3"];
arrSorts[8] = ["9", "细类三", "4", "3"];
arrSorts[9] = ["10", "小类四", "2", "2"];
arrSorts[10] = ["11", "小类五", "2", "2"];
arrSorts[11] = ["12", "小类六", "2", "2"];
arrSorts[12] = ["13", "细类四", "5", "3"];
arrSorts[13] = ["14", "细类五", "5", "3"];
arrSorts[14] = ["15", "末类一", "7", "4"];
arrSorts[15] = ["16", "末类二", "7", "4"];
arrSorts[16] = ["17", "末类三", "7", "4"];
arrSorts[17] = ["18", "终极类一", "15", "5"];
arrSorts[18] = ["19", "终极类二", "15", "5"];
arrSorts[19] = ["20", "终极类三", "15", "5"];
arrSorts[20] = ["21", "终极类四", "16", "5"];
arrSorts[21] = ["22", "终极类五", "16", "5"];
arrSorts[22] = ["23", "终极类六", "16", "5"];
arrSorts[23] = ["24", "末类四", "8", "4"];
arrSorts[24] = ["25", "末类五", "8", "4"];
arrSorts[25] = ["26", "末类六", "8", "4"];
arrSorts[26] = ["27", "末类七", "9", "4"];
arrSorts[27] = ["28", "末类八", "9", "4"];
arrSorts[28] = ["29", "末类九", "9", "4"];
arrSorts[29] = ["30", "终极类七", "17", "5"];
arrSorts[30] = ["31", "终极类八", "17", "5"];
arrSorts[31] = ["32", "终极类九", "17", "5"];
arrSorts[32] = ["33", "终极类十", "24", "5"];
arrSorts[33] = ["34", "终极类十一", "24", "5"];
arrSorts[34] = ["35", "终极类十二", "24", "5"];
arrSorts[35] = ["36", "终极类十三", "24", "5"];
  select用数组存放,为selectArr.js:
var selectArr=new Array();
selectArr.push("test1");
selectArr.push("test2");
selectArr.push("test3");
selectArr.push("test4");
selectArr.push("test5");
var selectArr2=new Array();
selectArr2.push("test6");
selectArr2.push("test7");
selectArr2.push("test8");
selectArr2.push("test9");
//selectArr2.push("test10");
  然后,就是主要联动处理类sort.js了:
/**
 * 无限级联动选择菜单类
 * _storeValueObjName:存放选择项值的页面元素名称
 * _showSelectObjName:显示该菜单的页面元素名称
 * _sortArr:显示菜单所需的数组,格式如下
 * arrSorts[0] = ["类别ID1", "类别一", "父类ID1", "深度1"];
 * arrSorts[1] = ["类别ID2", "类别二", "父类ID2", "深度2"];
 *
 * @author zxub 2005-8-23
 */
function sortMenu(_storeValueObjName, _sortArr, _selectArr)
{
 this.storeValueObj=document.getElementById(_storeValueObjName);
 this.sortArr=_sortArr;
    this.selectArr=_selectArr;
    this.maxDepth=_selectArr.length;

 /**
  * 获取第一层分类,并显示在showSelectObj中
  * _sortMenuObj:sortMenu的实例对象,指向自己
  */
 this.initSorts=function(_sortMenuObj)
 {
        for (i=1;i<this.maxDepth;i++)
        {
            document.getElementById(this.selectArr[i]).style.display="none";
        }
        this.storeValueObj.value=0;
        _select=document.getElementById(this.selectArr[0]);
        _select.sortMenuObj=_sortMenuObj;
        _select.onchange=function()
        {
            this.sortMenuObj.setSorts(this,this.sortMenuObj);
        }
        _select.add(new Option("请选择...",""));
  for (var i = 0; i < this.sortArr.length; i++)
  {
   if (this.sortArr[i][2] == 0)
   {
                _select.add(new Option(this.sortArr[i][1],this.sortArr[i][0]));
   }
  }
        _select.depth=1;
 }

    this.getSelectIndex=function(_selectName)
    {
        _index=null;
        for (i=0;i<this.maxDepth;i++)
        {
            if (this.selectArr[i]==_selectName)
            {
                _index=i;
                break;
            }
        }
        return _index;
    }

 /**
  * 下拉框联动
  * _curSelect:当前选择的下拉框
     * _sortMenuObj:sortMenu的实例对象,指向自己
  */
 this.setSorts=function(_curSelect,_sortMenuObj)
 {
        //若当前下拉框后面还有下拉框,即有下级下拉框时,清除下级下拉框,在后面会重新生成下级部分
        for (i=_curSelect.depth;i<this.maxDepth;i++)
        {
            _select=document.getElementById(this.selectArr[i]);
            while(_select.length>0)
                _select.remove(0);
            _select.style.display="none";
        }
        //获取当前选项的值
  _iValue = _curSelect.options[_curSelect.selectedIndex].value;
  //如果选择的是下拉框第一项(第一项的值为"")
  if (_iValue == "")
  {           
   //若存在上级下拉框
   if (_curSelect.depth>1)
   {
                _parent=document.getElementById(this.selectArr[_curSelect.depth-2]);
    //取值为上级下拉框选中值
    this.storeValueObj.value = _parent.options[_parent.selectedIndex].value;
   }
   else
   {
    //没上级则取值为0
    this.storeValueObj.value = 0;
   }           
   //选择第一项(请选择...),没有下级选项,所以要返回           
   return false;
  }
  //选择的不是第一项
  this.storeValueObj.value = _iValue;
        if (_curSelect.depth==_sortMenuObj.maxDepth)
        {
            return false;
        }
  //去掉当前下拉框原来的选择状态
        //将选中的选项对应代码更改为 selected
        for (i=0;i<_curSelect.options.length;i++)
        {
            if (_curSelect.options[i].selected=="selected")
            {
                _curSelect.options[i].removeAttribute("selected");
            }
            if (_curSelect.options[i].value==_iValue)
            {
                _curSelect.options[i].selected="selected";
            }
        }
        //新生成的下级下拉列表
        _hasChild=false;
        for (var i = 0; i < this.sortArr.length; i++)
  {
            if (this.sortArr[i][2] == _iValue)
            {
                if (_hasChild==false)
                {
                    _siblingSelect=document.getElementById(this.selectArr[_curSelect.depth]);
                    _siblingSelect.sortMenuObj=_sortMenuObj;
                    _siblingSelect.style.display="";
                    _siblingSelect.depth=parseInt(_curSelect.depth)+1;
                    _siblingSelect.onchange=function()
                    {
                        this.sortMenuObj.setSorts(this,this.sortMenuObj);
                    }
                    _siblingSelect.add(new Option("请选择...",""));
                    _siblingSelect.add(new Option(this.sortArr[i][1],this.sortArr[i][0]));
                    _hasChild=true;
                }
                else
                {                  
                    _siblingSelect.add(new Option(this.sortArr[i][1],this.sortArr[i][0]));
                }
            }
        }
 }

 /**
  * 根据最小类选取值生成整个联动菜单,由后往前递归完成
  * _minCataValue:最小类的取值
     * _sortMenuObj:sortMenu的实例对象,指向自己
  */
    this.hideChildInNewInit=false;
 this.newInit=function(_minCataValue,_sortMenuObj)
 {
        if (this.storeValueObj.value=="undefined" || this.storeValueObj.value=="")
        {
            this.storeValueObj.value=_minCataValue;
        }
  if (_minCataValue == 0)
  {
   //minCataValue为0,也就是初始化了
   this.initSorts(_sortMenuObj);
   //初始化完成后,退出函数
   return false;
  }
        //父级ID
  _parentID=null;
        _depth=null;
        for (var i = 0; i < this.sortArr.length; i++)
  {
   if (_minCataValue == this.sortArr[i][0])
   {
                _depth=this.sortArr[i][3];
    _parentID = this.sortArr[i][2];
    break;
   }
  }
        if (!this.hideChildInNewInit)
        {
            for (i=_depth;i<this.maxDepth;i++)
            {
                document.getElementById(this.selectArr[i]).style.display="none";
            }
            this.hideChildInNewInit=true;
        }       
        if (_depth>this.maxDepth)
        {
            alert("下拉框只能构成"+this.maxDepth+"级关系,而当前值处于第"+_depth+"级关系中!");
            alert("初始化下拉框为原始状态");
            this.initSorts(_sortMenuObj);
            return false;
        }
  _select=document.getElementById(this.selectArr[_depth-1]);
        _select.depth=_depth;
        _select.sortMenuObj=_sortMenuObj;
        _select.onchange=function()
        {
            this.sortMenuObj.setSorts(this,this.sortMenuObj);
        }
        _select.add(new Option("请选择...","")); 
  
  for (var i = 0; i < this.sortArr.length; i++)
  {
   if (this.sortArr[i][2] == _parentID)
   {
    if (this.sortArr[i][0] == _minCataValue)
    {
                    _opt=new Option(this.sortArr[i][1],this.sortArr[i][0]);
                    _select.add(_opt);
                    _opt.selected="selected";
    }
    else     
    {
                    _select.add(new Option(this.sortArr[i][1],this.sortArr[i][0]));
                }
   }
  }  
  if (_parentID > 0)
  {
   this.newInit(_parentID,_sortMenuObj);
  }
 }
}
  最后是测试用页面test.htm:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language="javascript" src="sortArr.js"></script>
<script language="javascript" src="selectArr.js"></script>
<script language="javascript" src="sort.js"></script>
</HEAD>

<BODY>
<select name=test1></select>
<select name=test2></select>
<select name=test3></select>
<select name=test4></select>
<select name=test5></select>
<input id=testValue name=testValue><br>
<select name=test6></select>
<select name=test7></select>
<select name=test8></select>
<select name=test9></select>
<!--<select name=test10></select>-->
<input id=testValue2 name=testValue2>
<script language="javascript">
  var a=new sortMenu("testValue",arrSorts,selectArr);
  a.initSorts(a);
  var b=new sortMenu("testValue2",arrSorts,selectArr2);
  b.newInit(7,b);
</script>
</BODY>
</HTML>
  好了,可指定select的无限联动下拉框就到这里了,后面有select可编辑的这种联动,嘿嘿,下面见,闪~

 posted on 2005-10-12 22:08  往事如风  阅读(1243)  评论(0编辑  收藏  举报