html5中文学习网

您的位置: 首页 > 网络编程 > ASP编程 » 正文

三阶(三级)联动下拉选单_ASP教程_编程技术

[ ] 已经帮助:人解决问题
下拉
  最近常被的就是如何建立下拉式 (二),一月之被了不下十次,其要做到下拉式 (二) 不困,但是要和料合,把料的料以下拉式建立就必要加上一技巧了,今天者也不打算做二的下拉式,乾脆直接挑三,再把料一起合,大家了解的透一。

  首先先我看看下拉式(三) 的原始程式,等你看懂了之後咱再利用 ASP 和 SQL 生下拉式 (三):

  下拉式 (三):

  表名 myForm

  第一下拉式名 color

  色:

  色">色

  色">色

  色">色

  第二下拉式名 fruit

  水果:

  果

  

  李子

  第三下拉式名 price

  格:

  10元

  20元

  30元

  定色下拉式一一列,且列度三(表示有三色)

  key=new Array(3);

  //定色下拉式中第一色一一列,且列度三(表示有三水果),以下依此推

  key[0]=new Array(3);

  key[1]=new Array(2);

  key[2]=new Array(3);

  //重定一列是了在指定水果下拉式中之水果名已把水果名

  //指定相的列索引,些列索引法被重定成列,了要格下拉式

  //能沿用之前所定的列值,所以我在此另外再定一一模一的列索引。

  key1=new Array(3);

  key1[0]=new Array(3);

  key1[1]=new Array(2);

  key1[2]=new Array(3);


  //定水果下拉式中第一水果一一列,且列度三(表示有三

  
//),此水果是於色下拉式中之第一色,以下依此推

  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[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][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元"

  //改色下拉式之值, Buildkey 函,色下拉式中之

  
//索引值 num ,以本例而言,色 0 的索引值,色 1 的索引

  
//值,色 2 的索引值

  


  function Buildkey(num)

  {

  //水果下拉式之指索引指定0。

  document.myForm.fruit.selectedIndex=0;

  //由於改水果下拉式,格下拉式也必跟著,所以我在此便呼

  
//Buildkey1函,迫格下拉式中之索引值 0 ,表示不管目前是何

  
//色的水果,格下拉式都呈此水果的第一格

  


  Buildkey1(0);

  //是最的步,在 JavaScript 中,for(ctr=0;ctr//[color=#0000ff]就等於 VBScript 中的 For ctr = 0 to key[num].length,若有不懂的地方建大

  
//一本 JavaScript 的看,例明底下的程式大家比清楚,我

  
//了色之後,此 num=0 (前面已了),此 key[0]的列度我可以透

  
//前定得知, key[0].length=3,而水果下拉式始改,水果下拉式中

  
//第一key[0][0],也就是果,水果下拉式中第二成key

  
//[0][1],也就是,水果下拉式中第三成key[0][2],也就是李子,

  
//大家了解了吧!!

  


  for(ctr=0;ctrkey[0].length指定目前水果下拉式之索引列度。

  document.myForm.fruit.length=key[num].length;

  }

  function Buildkey1(num)

  {

  //格下拉式之指索引指定0。

  document.myForm.price.selectedIndex=0;

  //改格下拉式的除了跟水果下拉式相之外,跟目前所色有

  
//例明底下的程式大家比清楚,我了色之後,此

  
//document.myForm.color.selectedIndex=0 (因色是色下拉式中之第

  
//一指索引值),如果我在水果下拉式中的水果是,此 num=1 ,而

  
//key1[document.myForm.color.selectedIndex][num].length 此成 key1

  //[0][1].length,之前的定可以知道值是3,而格下拉式始改,格//下拉式中第一key[0][1][0],也就是40 元,格下拉式中第二

  
//key[0][1][1],也就是50 元,水果下拉式中第三成key

  
//[0][1][2],也就是60 元,大家了解了吧!!

  for(ctr=0;ctr由色下拉式索引取得水果列值;

  document.myForm.cost.options[ctr]=new Option(key1[document.myForm.color.selectedIndex][num][ctr],key1[document.myForm.color.selectedIndex][num][ctr]);

  }

  }

  //-->

  上面的程式希望大家用值去的方式,我想比好,若有任何看不懂的地方可以直接留言告我,接下我始利用 ASP SQL 生上面那段程式,希望大家不要因上面的程式看不太懂而退,因底下的程式更,如果大家能整流程吸收,我相信不管是下拉式 (四)或下拉式 (五)都不倒你了,之所以用上面的方式建立下拉式是因者得例然比麻,但是是比容易理解的,另外方式者得可能出之後不好解所以作!!

在入作之前,先我建立三料表,一是 color 料表,一是 fruit 料表,一是 price 料表,料表架如下:
三阶(三级)联动下拉选单


三料表容分如下所示:
三阶(三级)联动下拉选单
 
三阶(三级)联动下拉选单
 
三阶(三级)联动下拉选单

  大家可以看的到 fruit 料表是以 COLORID 位 color 料表形成,而 price 料表又以 FRUITID 位 fruit 料表形成,我想要料是以下面的方式呈如何做呢?
色→果→10元
色→果→20元
色→果→30元
色→→40元
色→→50元
色→→60元
色→李子→70元
色→李子→80元
色→李子→90元
色→柳丁→100元
色→柳丁→110元
色→柳丁→120元
色→葡萄柚→130元
色→葡萄柚→140元
色→葡萄柚→150元
色→芭→160元
色→芭→170元
色→芭→180元
色→西瓜→190元
色→西瓜→200元
色→西瓜→210元
色→子→220元
色→子→230元
色→子→240元

  我用 JOIN 的方式三料表起, JOIN 的是把 fruit 料表中 COLORID 位值等於 color 料表中 ID 位的料且 price 料表中 FRUITID 位值等於 fruit 料表中 ID 位的料 JOIN 起。
<%
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"

Set my_conn = Server.CreateObject("ADODB.Connection")
my_conn.open myDSN
set rs=my_conn.execute(strSQL)
DO UNTIL rs.eof
Response.Write rs(0) & "→" & rs(1) & "→" & rs(2) & "<br>"
rs.movenext

LOOP

rs.close
set rs=nothing
my_conn.close
set my_conn=nothing

%>

三阶(三级)联动下拉选单



  就上面的料架而言我可以很清楚了看到,色→果→10元相於是 key1[0][0][0] 列索引的值,而色→子→240元就相於是 key1[2][2][2] 列索引的值,而色→果也相於是 key[0][0] 列索引的值,注意一是 key ,一是 key1,也就是我在文章一始告你何要定列值的原因, OK ,看了料的架之後最麻的事情了,要如何把的料架成列,而且要和料作合呢?底下一段程式可能有,不我量解清楚:

  DynamicSelectBox.asp

  


  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")

  %>

  
:

  
水果:

  
:

  -->

  "

  
'利用 fruitlist 字串建立第二下拉式的 Html 原始。

  
fruitlist=""

  '
利用 pricelist 字串建立第三下拉式的 Html 原始。

  
pricelist=""

  
'在初始某些值,作底下行算之旗值。

  
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 thisvalue2lastvalue2 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 thisvaluelastvalue 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 & "" & thisvalue & "" & 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 & "" & thisvalue2 & "" & vbcrlf

  end if

  End If

  
'如果目前是第一色,而且是第一水果,就格到 price 下拉式。

  if howmanygroups=0 then

  if howmanysubgroups=0 then

  pricelist = pricelist & "" & thisvalue3 & "" & 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 & ""

  
' fruit 下拉式作一尾

  fruitlist= fruitlist & ""

  
' price 下拉式作一尾

  pricelist= pricelist & ""

  
'一始的 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

  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


  再大家看看最後的果:

  色:

  色

  色

  色

  水果:

  果

  

  李子

  格:

  10元

  20元

  30元

  送出">

  色

  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);

  // 果

  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元";

  function Buildkey(num)

  {

  Buildkey1(0);

  document.myForm.fruit.selectedIndex=0;

  for(ctr=0;ctr

其程式不,重是在列索引的念,同的有可以在直接提出,希望大家看了篇文章之後,不要再我下拉式 (二)如何做了!!祝大家身健康, Coding 快。

wkKHTML5中文学习网 - HTML5先行者学习网
wkKHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助