Asp/asp.net中下拉列表的N级联动

 <form runat="server">
  州
    
<asp:DropDownList ID="DDListCountry" runat="server">  </asp:DropDownList>
    国
    
<asp:DropDownList ID="DDListProvince" runat="server"></asp:DropDownList>
    省
    
<asp:DropDownList ID="DDListCity" runat="server"></asp:DropDownList>
    市
    
<asp:DropDownList ID="DDListVillage" runat="server"></asp:DropDownList>
  
</form>


<script language="javascript">
function chgitem(sID1,sID2)    //改变子类内容
{
var obj1=document.getElementById(sID1);
var obj2=document.getElementById(sID2);
obj2.options.length
=0;
obj2.options.add(
new Option("请选择","请选择"));
var parentstr=obj1.options[obj1.options.selectedIndex].text;
obj2.focus();
for(var i=0;i<array.length;i++)
    
{
    
if(array[i][0]==parentstr) obj2.options.add(new Option(array[i][1],array[i][1]));
    }

}


function relating(obj1,obj2)    //关联Dropdownlist控件
{    
this.objparent=document.getElementById(obj1);
this.objsub=document.getElementById(obj2);
this.objparent.onchange=new Function("chgitem('"+obj1+"','"+obj2+"')");    
this.objsub.focus();
}


function isexists(sID,str)      //测试字符串在下拉列表中是否存在
{
var obj=document.getElementById(sID);
for(var i=0;i<obj.options.length;i++)
    
if(obj.options[i].text==str) return true;
return false;
}


function initializearray(sID,arr,index,flag)  //初始化0级
{
    
    
this.obj=document.getElementById(sID);
    
this.obj.options.length=0;
    
this.obj.options.add(new Option("请选择","请选择"));
    
    
for(var i=0;i<arr.length;i++)
        
{
        
if(arr[i][2]==flag&&isexists(sID,arr[i][index])==false)
            
this.obj.options.add(new Option(arr[i][index],arr[i][index]));
            
        }

    
}



var array=new Array(); //数据数组
array[0]=new Array("亚洲","中国",0);
array[
1]=new Array("亚洲","日本",0);
array[
2]=new Array("亚洲","土耳其",0);
array[
3]=new Array("中国","湖北");
array[
4]=new Array("中国","湖南");
array[
5]=new Array("湖北","随州");
array[
6]=new Array("随州","曾都");


initializearray(
"DDListCountry",array,0,0);
relating(
"DDListCountry","DDListProvince");
relating(
"DDListProvince","DDListCity");
relating(
"DDListCity","DDListVillage");


</script>

posted on 2006-03-20 12:57  痛并快乐着  阅读(1495)  评论(0编辑  收藏  举报

导航