建立动态下拉式选单(三阶层)

'建立動態下拉式選單 (三階層)

1'将资料库连结字串, SQL 叙述,形成第一个表单字串变数 (colorlist),第一个表单名称 'color,形成第二个表单字串变数 (fruitlist),第二一个表单名称 fruit,形成第三个表单字串变数 '(pricelist),第三个表单名称 price,等资讯透过呼叫副程式的方式经过处理之后再传回主程式'中,请注意有某几个参数是以 ByRef 的方式传回主程式,而某些参数是以 ByVal 的方式传回'的紫色粗体字代表是 ByRef。请直接观看底下副程式的部分   
2myDSN="Provider=SQLOLEDB; Data Source=Jackal; Initial Catalog=pubs; User ID=sa; Password="   
3strSQL="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"   
4call listmaker(myDSN, strSQL, colorlist,"color", fruitlist ,"fruit", pricelist, "price", pubevent, pubfun, "myForm")
1<form action="listbox2.asp" method="post" name="myForm">   
2颜色:   

= colorlist

1  
2<br/>   
3水果:   
4  
5  

= fruitlist

1  
2<br/>   
3价格:   

= pricelist

1<input type="submit" value="送出"/>
2</form>
1<script language="JavaScript"><!--   
2  

=pubevent

=pubfun

1\--></script>
1   
2  
3SUB listmaker(myDSN, strSQL,byref list1, listname1, byref list2, listname2, byref list3, listname3, byref myevent, byref myfun, myForm)   
4Set my_conn = Server.CreateObject("ADODB.Connection")   
5my_conn.open myDSN   
6set rs=my_conn.execute(strSQL)   
7'利用 colorlist 字串变数来建立第一个下拉式选单的 Html 原始码。   
8colorlist="

<select "(this.selectedindex);""="" &="" buildkey"="" colorlist="colorlist" name=" &amp; listname1
colorlist=colorlist &amp; " onchange="">"
'利用 fruitlist 字串变数来建立第二个下拉式选单的 Html 原始码。
fruitlist="<select "(this.selectedindex);""="" &="" buildkey1"="" fruitlist="fruitlist" name=" &amp; listname2
fruitlist=fruitlist &amp; " onchange="">"
'利用 pricelist 字串变数来建立第三个下拉式选单的 Html 原始码。
pricelist="<select name=" &amp; listname3 &amp; ">"
'在这里初始某些变数值,当作底下执行运算时之旗标值。
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&lt;&gt;lastvalue2 then
If howmanysubgroups=0 then
tempSTR1=tempSTR1 &amp; "// " &amp; lastvalue &amp; vbcrlf
tempSTR2=tempSTR2 &amp; "// " &amp; lastvalue &amp; vbcrlf
end If
tempSTR2=tempSTR2 &amp; "key1[" &amp; howmanygroups &amp; "][" &amp; howmanysubgroups &amp;
"]=new Array(" &amp; thissubgroupcount &amp; ");" &amp; vbcrlf
tempSTR1=tempSTR1 &amp; "key[" &amp; howmanygroups &amp; "][" &amp; howmanysubgroups &amp;
"]=""" &amp; lastvalue2 &amp; """;" &amp; 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&lt;&gt;lastvalue then
tempSTR=tempSTR &amp; "key[" &amp; howmanygroups &amp; "]=new Array(" &amp; howmanysubgroups &amp; ");" &amp; _vbcrlf
tempSTR=tempSTR &amp; "key1[" &amp; howmanygroups &amp; "]=new Array(" &amp; howmanysubgroups &amp; ");" &amp; _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 &amp; "<option>" &amp; thisvalue &amp; "</option>" &amp; vbcrlf
end if
'如果 thissubgroupcount=0 代表目前水果改变,此时在key1[第几种颜色][第几种水果][第几'种价格] 阵列值前加上水果名称标记,让大家更容易将水果名称与价格区隔开来。
if thissubgroupcount=0 then
tempSTR3=tempSTR3 &amp; "// " &amp; lastvalue2 &amp; vbcrlf
end if
'定义key1[第几种颜色][第几种水果][第几种价格] 阵列值。
tempSTR3=TempSTR3 &amp; "key1[" &amp; howmanygroups &amp; "][" &amp; howmanysubgroups &amp; "][" &amp; thissubgroupcount &amp; "]=""" &amp; thisvalue3 &amp; """;" &amp; 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 &amp; "<option>" &amp; thisvalue2 &amp; "</option>" &amp; vbcrlf
end if
End If

'如果目前是第一种颜色,而且是第一种水果,就将价格写到 price 下拉式选单。
if howmanygroups=0 then
if howmanysubgroups=0 then
pricelist = pricelist &amp; "<option>" &amp; thisvalue3 &amp; "</option>" &amp; vbcrlf
end if
End if

thisgroupcount=thisgroupcount+1
thissubgroupcount=thissubgroupcount+1
lastvalue=thisvalue
loopcounter=loopcounter+1
rs.movenext

LOOP
'别忘了最后一个 key[第几种颜色] 和 key1[第几种颜色] 的阵列长度还未定义。
tempSTR=tempSTR &amp; "key[" &amp; howmanygroups &amp; _
"]=new Array(" &amp; howmanysubgroups+1 &amp; ");" &amp; _
vbcrlf
tempSTR=tempSTR &amp; "key1[" &amp; howmanygroups &amp; _
"]=new Array(" &amp; howmanysubgroups+1 &amp; ");" &amp; _
vbcrlf

'别忘了最后一个 key[第几种颜色][第几种水果] 和 key1[第几种颜色][第几种水果] 的阵列长'度还未定义。
tempSTR2=TempSTR2 &amp;"key1[" &amp; howmanygroups &amp; "][" &amp; howmanysubgroups &amp; "]=new Array(" &amp; thissubgroupcount &amp; ");" &amp; _vbcrlf
tempSTR1= TempSTR1 &amp; "key[" &amp; howmanygroups &amp; "][" &amp; howmanysubgroups &amp; "]=""" &amp; lastvalue2 &amp; """;" &amp; vbCRLF tempSTR=tempSTR &amp; TempSTR1 &amp; TempSTR2 &amp; tempSTR3
'将 color 下拉式选单作一个结尾
colorlist=colorlist &amp; "</select>"
'将 fruit 下拉式选单作一个结尾
fruitlist= fruitlist &amp; "</select>"
'将 price 下拉式选单作一个结尾
pricelist= pricelist &amp; "</select>

 1"   
 2  
 3'一开始的 key 和 key1 阵列也不要忘记定义了。   
 4  
 5myevent=vbcrlf & "key=new Array(" & howmanygroups+1 & ");"   
 6myevent=myevent & vbcrlf & "key1=new Array(" & howmanygroups+1   
 7myevent=myevent & ");" & vbcrlf & tempSTR   
 8  
 9'大功告成,可以关闭资料库了。   
10rs.close   
11set rs=nothing   
12my_conn.close   
13set my_conn=nothing   
14  
15'先产生 Buildkey 副程式,这边一点都不困难,原理在上面已经解释过了,笔者不再赘述,执行结果为:   
16function Buildkey(num)   
17{   
18Buildkey1(0);   
19document.myForm.fruit.selectedIndex=0;   
20for(ctr=0;ctr<key[num].length;ctr++)   
21{   
22document.myForm.fruit.options[ctr]=new Option(key[num][ctr],key[num][ctr]);   
23}   
24document.myForm.fruit.length=key[num].length;   
25}   
26  
27tempSTR =vbcrlf & "function Buildkey" & "(num)" & vbcrlf   
28tempSTR =tempSTR & "{" & vbcrlf   
29tempSTR =tempSTR & "Buildkey1(0);" & vbcrlf   
30tempSTR =tempSTR & "document." & myForm & "."   
31tempSTR =tempSTR & listname2 & ".selectedIndex=0;" & vbcrlf   
32tempSTR =tempSTR & "for(ctr=0;ctr<key[num].length;ctr++)" & vbcrlf   
33tempSTR =tempSTR & "{" & vbcrlf   
34tempSTR =tempSTR & "document." & myform & "." & listname2   
35tempSTR =tempSTR & ".options[ctr]=new Option(key[num][ctr],"   
36tempSTR =tempSTR & "key[num][ctr]);" & vbcrlf   
37tempSTR =tempSTR & "}" & vbcrlf   
38tempSTR =tempSTR & "document." & myForm & "." & listname2   
39tempSTR =tempSTR & ".length=key[num].length;" & vbcrlf   
40tempSTR =tempSTR & "}" & vbcrlf   
41  
42'再产生 Buildkey1 副程式   
43function Buildkey1(num)   
44{   
45document.myForm.price.selectedIndex=0;   
46for(ctr=0;ctr<key1[document.myForm.color.selectedIndex][num].length;ctr++){   
47document.myForm.price.options[ctr]=new Option(key1[document.myForm.color.selectedIndex][num][ctr],key1[document.myForm.color.selectedIndex][num][ctr]);   
48}   
49document.myForm.price.length=key1[document.myForm.color.selectedIndex][num].length;   
50}   
51  
52tempSTR =tempSTR & vbcrlf & "function Buildkey1" & "(num)" & vbcrlf   
53tempSTR =tempSTR & "{" & vbcrlf   
54tempSTR =tempSTR & "document." & myForm & "."   
55tempSTR =tempSTR & listname3 & ".selectedIndex=0;" & vbcrlf   
56tempSTR =tempSTR & "for(ctr=0;ctr<key1[document.myForm.color.selectedIndex][num].length;ctr++)" & vbcrlftempSTR =tempSTR & "{" & vbcrlf   
57tempSTR =tempSTR & "document." & myform & "." & listname3   
58tempSTR =tempSTR & ".options[ctr]=new Option(key1[document.myForm.color.selectedIndex][num][ctr],"   
59tempSTR =tempSTR & "key1[document.myForm.color.selectedIndex][num][ctr]);" & vbcrlftempSTR =tempSTR & "}" & vbcrlf   
60tempSTR =tempSTR & "document." & myForm & "." & listname3   
61tempSTR =tempSTR & ".length=key1[document.myForm.color.selectedIndex][num].length;" & vbcrlftempSTR =tempSTR & "}" & vbcrlf   
62myfun=tempSTR   
63END sub   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus