'建立動態下拉式選單 (三階層)
<%'將資料庫連結字串, SQL 敘述,形成第一個表單字串變數 (colorlist),第一個表單名稱 'color,形成第二個表單字串變數 (fruitlist),第二一個表單名稱 fruit,形成第三個表單字串變數 '(pricelist),第三個表單名稱 price,等資訊透過呼叫副程式的方式經過處理之后再傳回主程式'中,請注意有某幾個參數是以 ByRef 的方式傳回主程式,而某些參數是以 ByVal 的方式傳回'的紫色粗體字代表是 ByRef。請直接觀看底下副程式的部分 myDSN="Provider=SQLOLEDB; Data Source=Jackal; Initial Catalog=pubs; User ID=sa; Password=" strSQL="select color.color, fruit.fruit, price.cost from color join fruit on color.ID=fruit.COLORID join price on price.FRUITID=fruit.ID order by color.ID" call listmaker(myDSN, strSQL, colorlist,"color", fruitlist ,"fruit", pricelist, "price", pubevent, pubfun, "myForm")%><form name="myForm" method=post action=listbox2.asp> 顏色: <%= colorlist%>
<BR> 水果:
<%= fruitlist%>
<BR> 價格: <%= pricelist%> <input type=submit value="送出"> </Form> <SCRIPT Language="JavaScript"><!--
<%=pubevent%> <%=pubfun%> --></Script>
<%
SUB listmaker(myDSN, strSQL,byref list1, listname1, byref list2, listname2, byref list3, listname3, byref myevent, byref myfun, myForm) Set my_conn = Server.CreateObject("ADODB.Connection") my_conn.open myDSN set rs=my_conn.execute(strSQL) '利用 colorlist 字串變數來建立第一個下拉式選單的 Html 原始碼。 colorlist="<select name=" & listname1 colorlist=colorlist & " OnChange=""Buildkey" colorlist=colorlist & "(this.selectedIndex);"">" '利用 fruitlist 字串變數來建立第二個下拉式選單的 Html 原始碼。 fruitlist="<select name=" & listname2 fruitlist=fruitlist & " OnChange=""Buildkey1" fruitlist=fruitlist & "(this.selectedIndex);"">" '利用 pricelist 字串變數來建立第三個下拉式選單的 Html 原始碼。 pricelist="<select name=" & listname3 & ">" '在這里初始某些變數值,當作底下執行運算時之旗標值。 loopcounter=0 lastvalue=rs(0) lastvalue2=rs(1) redim tempArray(1) 'thisgroupcount 代表某種顏色共有幾種水果 thisgroupcount=0 'thissubgroupcount 代表某種水果共有幾種價格,當水果變換時 thissubgroupcount 會歸零,并重新'累加。 thissubgroupcount=0 'howmanygroups 代表共有幾種顏色 howmanygroups=0 'howmanysubgroups 代表共有幾種水果,當顏色變換時 howmanysubgroups 會歸零,并重新'累加。 howmanysubgroups=0 DO UNTIL rs.eof thisvalue=rs(0) thisvalue2=rs(1) thisvalue3=rs(2)
'如果發現水果名稱改變,而且目前水果是某種顏色的第一種水果時,譬如:紅色←蘋果 '或:黃色←柳丁時就在 Script 中加上顏色標記 (為了讓大家更容易將 Script 區隔開'來),然后再將水果名稱定義到 key 陣列值, '接著再定義 key1[第幾種顏色][第幾種水果] 之陣列長度。
if thisvalue2<>lastvalue2 then If howmanysubgroups=0 then tempSTR1=tempSTR1 & "// " & lastvalue & vbcrlf tempSTR2=tempSTR2 & "// " & lastvalue & vbcrlf end If tempSTR2=tempSTR2 & "key1[" & howmanygroups & "][" & howmanysubgroups & "]=new Array(" & thissubgroupcount & ");" & vbcrlf tempSTR1=tempSTR1 & "key[" & howmanygroups & "][" & howmanysubgroups & "]=""" & lastvalue2 & """;" & vbCRLF howmanysubgroups=howmanysubgroups+1 thissubgroupcount=0 lastvalue2=thisvalue2 end if '執行結果如下 // 紅色 key[0][0]="蘋果"; key[0][1]="蓮霧"; key[0][2]="李子"; // 黃色 key[1][0]="柳丁"; key[1][1]="葡萄柚"; // 綠色 key[2][0]="芭樂"; key[2][1]="西瓜"; key[2][2]="棗子"; // 紅色 key1[0][0]=new Array(3); key1[0][1]=new Array(3); key1[0][2]=new Array(3); // 黃色 key1[1][0]=new Array(3); key1[1][1]=new Array(3); // 綠色 key1[2][0]=new Array(3); key1[2][1]=new Array(3); key1[2][2]=new Array(3);
'如果發現顏色名稱改變,就定義 key[第幾種顏色] 陣列值, '接著再定義 key1[第幾種顏色] 之陣列長度,重復定義的原因在之前已跟大家說過了,然'后將howmanygroups (顏色種類) 值加一,并將水果種類計數值 (thisgroupcount) 歸零if thisvalue<>lastvalue then tempSTR=tempSTR & "key[" & howmanygroups & "]=new Array(" & howmanysubgroups & ");" & _vbcrlf tempSTR=tempSTR & "key1[" & howmanygroups & "]=new Array(" & howmanysubgroups & ");" & _vbcrlf thisgroupcount=0 howmanygroups=howmanygroups+1 howmanysubgroups=0 end if
'執行結果如下 key=new Array(3); key1=new Array(3); key[0]=new Array(3); key1[0]=new Array(3); key[1]=new Array(2); key1[1]=new Array(2); key[2]=new Array(3); key1[2]=new Array(3) '如果 thisgroupcount=0 代表目前顏色改變,此時將顏色名稱寫到 color 下拉式選單中if thisgroupcount=0 then colorlist =colorlist & "<option>" & thisvalue & "</option>" & vbcrlf end if '如果 thissubgroupcount=0 代表目前水果改變,此時在key1[第幾種顏色][第幾種水果][第幾'種價格] 陣列值前加上水果名稱標記,讓大家更容易將水果名稱與價格區隔開來。 if thissubgroupcount=0 then tempSTR3=tempSTR3 & "// " & lastvalue2 & vbcrlf end if '定義key1[第幾種顏色][第幾種水果][第幾種價格] 陣列值。 tempSTR3=TempSTR3 & "key1[" & howmanygroups & "][" & howmanysubgroups & "][" & thissubgroupcount & "]=""" & thisvalue3 & """;" & vbCRLF '執行結果: // 頻果 key1[0][0][0]="10元"; key1[0][0][1]="20元"; key1[0][0][2]="30元"; // 蓮霧 key1[0][1][0]="40元"; key1[0][1][1]="50元"; key1[0][1][2]="60元"; // 李子 key1[0][2][0]="70元"; key1[0][2][1]="80元"; key1[0][2][2]="90元"; // 柳丁 key1[1][0][0]="100元"; key1[1][0][1]="110元"; key1[1][0][2]="120元"; // 葡萄柚 key1[1][1][0]="130元"; key1[1][1][1]="140元"; key1[1][1][2]="150元"; // 芭樂 key1[2][0][0]="160元"; key1[2][0][1]="170元"; key1[2][0][2]="180元"; // 西瓜 key1[2][1][0]="190元"; key1[2][1][1]="200元"; key1[2][1][2]="210元"; // 棗子 key1[2][2][0]="220元"; key1[2][2][1]="230元"; key1[2][2][2]="240元";
'如果目前是第一種顏色,而且水果名稱正在改變,就將水果名稱寫到 fruit 下拉式選單中 if howmanygroups=0 then If thissubgroupcount=0 then fruitlist = fruitlist & "<option>" & thisvalue2 & "</option>" & vbcrlf end if End If
'如果目前是第一種顏色,而且是第一種水果,就將價格寫到 price 下拉式選單。 if howmanygroups=0 then if howmanysubgroups=0 then pricelist = pricelist & "<option>" & thisvalue3 & "</option>" & vbcrlf end if End if
thisgroupcount=thisgroupcount+1 thissubgroupcount=thissubgroupcount+1 lastvalue=thisvalue loopcounter=loopcounter+1 rs.movenext
LOOP '別忘了最后一個 key[第幾種顏色] 和 key1[第幾種顏色] 的陣列長度還未定義。 tempSTR=tempSTR & "key[" & howmanygroups & _ "]=new Array(" & howmanysubgroups+1 & ");" & _ vbcrlf tempSTR=tempSTR & "key1[" & howmanygroups & _ "]=new Array(" & howmanysubgroups+1 & ");" & _ vbcrlf
'別忘了最后一個 key[第幾種顏色][第幾種水果] 和 key1[第幾種顏色][第幾種水果] 的陣列長'度還未定義。 tempSTR2=TempSTR2 &"key1[" & howmanygroups & "][" & howmanysubgroups & "]=new Array(" & thissubgroupcount & ");" & _vbcrlf tempSTR1= TempSTR1 & "key[" & howmanygroups & "][" & howmanysubgroups & "]=""" & lastvalue2 & """;" & vbCRLF tempSTR=tempSTR & TempSTR1 & TempSTR2 & tempSTR3 '將 color 下拉式選單作一個結尾 colorlist=colorlist & "</select>" '將 fruit 下拉式選單作一個結尾 fruitlist= fruitlist & "</select>" '將 price 下拉式選單作一個結尾 pricelist= pricelist & "</select>"
'一開始的 key 和 key1 陣列也不要忘記定義了。
myevent=vbcrlf & "key=new Array(" & howmanygroups+1 & ");" myevent=myevent & vbcrlf & "key1=new Array(" & howmanygroups+1 myevent=myevent & ");" & vbcrlf & tempSTR
'大功告成,可以關閉資料庫了。 rs.close set rs=nothing my_conn.close set my_conn=nothing
'先產生 Buildkey 副程式,這邊一點都不困難,原理在上面已經解釋過了,筆者不再贅述,執行結果為: function Buildkey(num) { Buildkey1(0); document.myForm.fruit.selectedIndex=0; for(ctr=0;ctr<key[num].length;ctr++) { document.myForm.fruit.options[ctr]=new Option(key[num][ctr],key[num][ctr]); } document.myForm.fruit.length=key[num].length; }
tempSTR =vbcrlf & "function Buildkey" & "(num)" & vbcrlf tempSTR =tempSTR & "{" & vbcrlf tempSTR =tempSTR & "Buildkey1(0);" & vbcrlf tempSTR =tempSTR & "document." & myForm & "." tempSTR =tempSTR & listname2 & ".selectedIndex=0;" & vbcrlf tempSTR =tempSTR & "for(ctr=0;ctr<key[num].length;ctr++)" & vbcrlf tempSTR =tempSTR & "{" & vbcrlf tempSTR =tempSTR & "document." & myform & "." & listname2 tempSTR =tempSTR & ".options[ctr]=new Option(key[num][ctr]," tempSTR =tempSTR & "key[num][ctr]);" & vbcrlf tempSTR =tempSTR & "}" & vbcrlf tempSTR =tempSTR & "document." & myForm & "." & listname2 tempSTR =tempSTR & ".length=key[num].length;" & vbcrlf tempSTR =tempSTR & "}" & vbcrlf
'再產生 Buildkey1 副程式 function Buildkey1(num) { document.myForm.price.selectedIndex=0; for(ctr=0;ctr<key1[document.myForm.color.selectedIndex][num].length;ctr++){ document.myForm.price.options[ctr]=new Option(key1[document.myForm.color.selectedIndex][num][ctr],key1[document.myForm.color.selectedIndex][num][ctr]); } document.myForm.price.length=key1[document.myForm.color.selectedIndex][num].length; }
tempSTR =tempSTR & vbcrlf & "function Buildkey1" & "(num)" & vbcrlf tempSTR =tempSTR & "{" & vbcrlf tempSTR =tempSTR & "document." & myForm & "." tempSTR =tempSTR & listname3 & ".selectedIndex=0;" & vbcrlf tempSTR =tempSTR & "for(ctr=0;ctr<key1[document.myForm.color.selectedIndex][num].length;ctr++)" & vbcrlftempSTR =tempSTR & "{" & vbcrlf tempSTR =tempSTR & "document." & myform & "." & listname3 tempSTR =tempSTR & ".options[ctr]=new Option(key1[document.myForm.color.selectedIndex][num][ctr]," tempSTR =tempSTR & "key1[document.myForm.color.selectedIndex][num][ctr]);" & vbcrlftempSTR =tempSTR & "}" & vbcrlf tempSTR =tempSTR & "document." & myForm & "." & listname3 tempSTR =tempSTR & ".length=key1[document.myForm.color.selectedIndex][num].length;" & vbcrlftempSTR =tempSTR & "}" & vbcrlf myfun=tempSTR END sub %>
|