asp 兩級下拉框聯動

来源:互联网 发布:阿里云服务器翻墙 编辑:程序博客网 时间:2024/04/29 15:49

<SCRIPT LANGUAGE="JavaScript">
<!--
function firstRecord(firstValue,firstText){
    this.firstValue = firstValue;
 this.firstText = firstText; 
}

function secondRecord(firstValue,firstText,secondValue,secondText,secondExtText){
     this.firstValue = firstValue;
     this.firstText = firstText;
     this.secondValue = secondValue;
     this.secondText = secondText;    
  this.secondExtText = secondExtText;    
}
function fillZipCode(oZipCode,oSecond){ 
  oZipCode.value = secondRecords[oSecond.value].secondExtText;
}
function changeFirstSelect(oFirst,oSecond,oZipCode){
 var selText = oFirst.value;
    oSecond.length=0;
    var iTmp = 0;
    for (i = 0;i < secondRecords.length; i++)  {
        if (secondRecords[i].firstValue == selText) {
            oSecond.options[iTmp] = new Option(secondRecords[i].secondText,i);
            iTmp ++ ;
        }
    }
    if (oSecond.length>0)
    {
        oSecond.options[0].selected = true;
  fillZipCode(oZipCode,oSecond);
    }else{
  oSecond.options.length = 0;
  oZipCode.value = "";
 }
}

function InitSel(firstValue,secondValue,oFirst,oSecond,oZipCode){
    var bHasSelected;
    bHasSelected = false;
 oFirst.options.length =0;
 oSecond.options.length =0;

    for (i = 0; i < firstRecords.length; i++)  {
        oFirst.options[i] = new Option(firstRecords[i].firstText,firstRecords[i].firstValue);  
        if (firstRecords[i].firstValue == firstValue ) {
            oFirst.options[i].selected = true;
            bHasSelected = true ;
        }
    } 
    if ( bHasSelected == false){
        if( oFirst.options.length>0)
            firstValue = oFirst.options[0].value;
        else
            return;
    }
 
 changeFirstSelect(oFirst,oSecond,oZipCode);
 
    bHasSelected = false;
    if (secondValue != "" ) {
        for (i = 0;i < oSecond.options.length; i++)  {
            if (secondRecords[oSecond.options[i].value].secondValue == secondValue) {     
                oSecond.options[i].selected = true;
                bHasSelected = true;
            }
        }
    }else{
        oSecond.options[0].selected = true;
        bHasSelected = true;
    }
    if (bHasSelected == false){
        oSecond.options[0].selected = true;
    }
 
 
}

//-->
</SCRIPT>

<%
'
    Function InitSelect(rs)
        response.write "<script>"
        response.write "var firstRecords = new Array();"
        response.write "var secondRecords = new Array();"
       
        If rs.recordcount = 0 Then

        ElseIf rs.recordcount = 1 Then
            %>
            firstRecords[0] = new firstRecord("<%=rs("CountyID")%>","<%=rs("County")%>");
            secondRecords[0] = new secondRecord("<%=rs("CityID")%>","<%=rs("City")%>","<%=rs("CountyID")%>","<%=rs("County")%>","<%=rs("ZipCode")%>");
            <%
        Else
            Dim pre,iTmp,i
            iTmp = 0
            pre = ""
            For i = 0 To rs.recordcount - 1
                If rs("CityID") = 0 Then
                    %>
                    firstRecords[<%=iTmp%>] = new firstRecord("<%=rs("CountyID")%>","<%=rs("County")%>");
                    <%
                    iTmp = iTmp + 1
                End If
                %>
                secondRecords[<%=i%>] = new secondRecord("<%=rs("CityID")%>","<%=rs("City")%>","<%=rs("CountyID")%>","<%=rs("County")%>","<%=rs("ZipCode")%>");
                <%               
                rs.movenext
            Next
        End If
        rs.close
        response.write "</script>"
  if err.number<>0 then
   InitSelect = false
  else
   InitSelect = true
  end if
    End Function
   
%>

因項目的不同,此代碼僅供參考。