ASP + JS three-level linkage drop-down menu [Call Database Data]
Source: Internet
Author: User
There are many third-level menus on the Internet, but the code is annoying. I should say this is more intuitive:
'The database must be connected first, not to mention
'Database Structure
'Category 1 Table Name: a field: ID, Name Description: ID is the ID value of category 1, and Name is the Name of Category 1
'Category 2 Table Name: aa field: ID, aID, Name Description: ID is the ID value of category 2, and aID is the ID value of category 1, name is the Name of Category 2
'Category 3 Table Name: aaa field: ID, aID, aaID, Name Description: ID as the primary key is the ID value of category 3, aID is the ID value of category 1, aaID is the ID value of category 2, and Name is the Name of Category 3
===== Test1.asp you can test and receive the class ID ====
<%
Response. write "1:" & Request. form ("s1") & "<BR>"
Response. write "2:" & Request. form ("s2") & "<BR>"
Response. write "3:" & Request. form ("s3") & "<BR>"
%> Copy codeThe Code is as follows: <! -- Three levels of linkage menu start -->
<Script language = "JavaScript">
<! --
<%
'Second-level data is saved to the array
Dim count2, rsClass2, sqlClass2
Set rsClass2 = server. createobject ("adodb. recordset ")
SqlClass2 = "select * from aa"
RsClass2.open sqlClass2, conn, 1, 1
%>
Var subval2 = new Array ();
// Array structure: Level 1 root value, level 2 root value, level 2 display value
<%
Count2 = 0
Do while not rsClass2.eof
%>
Subval2 [<% = count2 %>] = new Array ('<% = rsClass2 ("aID") %>', '<% = rsClass2 ("ID ") %> ',' <% = rsClass2 ("Name") %> ')
<%
Count2 = count2 + 1
RsClass2.movenext
Loop
RsClass2.close
%>
<%
'Third-level data is saved to the array
Dim count3, rsClass3, sqlClass3
Set rsClass3 = server. createobject ("adodb. recordset ")
SqlClass3 = "select * from aaa"
RsClass3.open sqlClass3, conn, 1, 1
%>
Var subval3 = new Array ();
// Array structure: second-level root value, third-level root value, and third-level display value
<%
Count3 = 0
Do while not rsClass3.eof
%>
Subval3 [<% = count3 %>] = new Array ('<% = rsClass3 ("aaID") %>', '<% = rsClass3 ("ID ") %> ',' <% = rsClass3 ("Name") %> ')
<%
Count3 = count3 + 1
RsClass3.movenext
Loop
RsClass3.close
%>
Function changeselect1 (locationid)
{
Document. form1.s2. length = 0;
Document. form1.s2. options [0] = new Option ('= select category = ','');
Document. form1.s3. length = 0;
Document. form1.s3. options [0] = new Option ('= select topic = ','');
For (I = 0; I <subval2.length; I ++)
{
If (subval2 [I] [0] = locationid)
{Document. form1.s2. options [document. form1.s2. length] = new Option (subval2 [I] [2], subval2 [I] [1]);}
}
}
Function changeselect2 (locationid)
{
Document. form1.s3. length = 0;
Document. form1.s3. options [0] = new Option ('= select topic = ','');
For (I = 0; I <subval3.length; I ++)
{
If (subval3 [I] [0] = locationid)
{Document. form1.s3. options [document. form1.s3. length] = new Option (subval3 [I] [2], subval3 [I] [1]);}
}
}
// -->
</Script>
<Form name = "form1" method = "post" action = "test1.asp">
Three-Level linkage:
<%
Dim count1, rsClass1, sqlClass1
Set rsClass1 = server. createobject ("adodb. recordset ")
SqlClass1 = "select * from"
RsClass1.open sqlClass1, conn, 1, 1
%>
<Select name = "s1" onChange = "changeselect1 (this. value)">
<Option >== select channel ==</option>
<%
Count1 = 0
Do while not rsClass1.eof
Response. write "<option value =" & rsClass1 ("ID") & ">" & rsClass1 ("Name") & "</option>"
Count1 = count1 + 1
RsClass1.movenext
Loop
RsClass1.close
%>
</Select>
<Select name = "s2" onChange = "changeselect2 (this. value)">
<Option >== select a category ==</option>
</Select>
<Select name = "s3">
<Option >== select a topic ==</option>
</Select>
<Input type = "submit" name = "Submit" value = "submit"> </form>
<! -- Three-level linkage menu ended -->
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service