请教一个动态显示数据库内容的问题

york_lin 2003-06-16 07:45:58
有这样一张表如下:
select * from table1
id name fied1 desc
1 aa hh
2 aa jj
3 aa kk
4 bb ll
5 bb oo

现在想做两个<select> </select>
<select name="select1" id="Id" >
<option value=2 ><%=rs.fields.item("name").value%></option>
</select>
<select name="select2" id="Id" >
<option value=2 ><%=rs.fields.item("field1").value%></option>
</select>

select1 存放name的值,select2 为field1的值
当select1的值改变的时候,select2中值列出该name 相关的field1的值。
如当选择name= aa 时,select2列出hh,jj,kk 动态显示,而不刷新页面。
请教该怎么做。
...全文
66 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
york_lin 2003-06-17
  • 打赏
  • 举报
回复
继续讨论 如果 数据有上千条的时候 采用数组 客户端还能承受的了吗,
或则有三 级联 四级联的情况会怎么样呢
fason 2003-06-16
  • 打赏
  • 举报
回复
这样的问题很多.你应该先学会搜一搜.这是我前几天写的,IE,nescape,mozilla下通过
<!--#include file="conn.inc"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<title>两级关联菜单</title>
</HEAD>
<BODY>
<!--
by fason(2003.4.5)
表名:pro_city
结构:id(自编) province(文本) city(文本)
注释:conn.inc为连接数据库文件
ie,netscape,mozilla下测试通过
-->
<SCRIPT LANGUAGE=javascript>
<!--
var prv=new Array()//省
var cty=new Array()//市
function init(){
<%
dim prv,cty,i,j
i=0
set prv=conn.execute("select distinct province from pro_city")
if not prv.eof then
do while not prv.eof
j=0
%>
prv[<%=i%>]="<%=trim(prv("province"))%>";
<%
set cty=conn.execute("select city from pro_city where province='"&trim(prv("province"))&"'")
if not cty.eof then%>
cty[<%=i%>]=new Array()
<%do while not cty.eof%>
cty[<%=i%>][<%=j%>]="<%=trim(cty("city"))%>"
<%j=j+1
cty.movenext
loop
end if
cty.close :set cty=nothing
i=i+1
prv.movenext
loop
end if
prv.close :set prv=nothing
%>
if(prv.length >0){
for(i=0;i<prv.length;i++)
with(document.frm.province)options[length]=new Option(prv[i],prv[i])
document.frm.province.onchange=new Function("go(this.selectedIndex)")
}
}

function go(sIndex){
with(document.frm.city){
length=0;
if(sIndex==0){options[0]=new Option("请选择");return}
for(i=0;i<cty[sIndex-1].length;i++)
options[length]=new Option(cty[sIndex-1][i],cty[sIndex-1][i])
}
}
window.onload=init
-->
</SCRIPT>
<form name=frm>
<SELECT name=province>
<OPTION selected>请选择</OPTION>
</SELECT>
<SELECT name=city>
<OPTION selected>请选择</OPTION>
</SELECT>
</form>
</BODY>
</HTML>
lsrzm 2003-06-16
  • 打赏
  • 举报
回复
<!--月影飞鸿2003-05-25 晚-->
<!--#include file = dbconn.asp-->
<body onload =" ChangeProvince(document.all.Province);">
<%
Dim Arr_City

sub CloseRs(rs)
Rs.close
set Rs = nothing
end sub

Sql_All = "Select Id,Name from prover"
set Rs_All = Server.CreateObject ("adodb.recordset")
Rs_All.Open Sql_All,conn,3,1

'动态生成一个内容为省的select
Response.Write "<Select name = Province id = Province onchange = ChangeProvince(this)>"
do while not Rs_All.EOF
%>
<option value = <%=Rs_All("Id")%>><%=Rs_All("Name")%>
<%
Rs_All.MoveNext
loop
Response.Write "</Select>"
'到此第一个select为止

CloseRs(Rs_All)

'在城市表city中选择所有记录。Id:城市Id;CId:省Id;Name:城市的名字。
'然后按照:省Id加上“,”加上城市Id加上“,”加上城市的名字组合成一个字符串;
'如:'1,2,济南',表示:省的Id是1,城市的Id是2,城市的名字是 济南。
'然后每一个类似的字符串作为一个一维数组的一个元素的值。
'如此循环,动态生成了一个数组,每一个下表的值为类似“1,2,济南”的字符串
Sql_City= "select Id,CId,Name from city order by Id"
set Rs_City = server.CreateObject ("adodb.recordset")
Rs_City.Open Sql_City,conn,3,1
count = Rs_City.RecordCount '得到动态数组的元素个数
redim Arr_City(count) '重新初始化动态数组
i = 0
do while not Rs_City.EOF
New_Str = ""
theId = Rs_City("Id") '得到城市Id
theCId = Rs_City("CId") '得到省的Id
theName = Rs_City("Name") '得到城市的名字
New_Str = theCId&","&theId&","&theName '组合成一个字符串
Arr_City(i) = New_Str '给数组赋值
i = i +1
Rs_City.MoveNext
loop

'动态数组的创建到此为止

CloseRs(Rs_City)
Conn.Close
set Conn = Nothing

'然后把上边动态生成的数组Arr_City赋值给javascipt中的一个数组:All_City
Response.Write "<script language = javascript>"
Response.Write " var All_City = new Array("&count&");"
for i = lbound(Arr_City) to ubound(Arr_City)-1
Response.Write "All_City["&i&"] = '"&Arr_City(i)&"';"
next
Response.Write "</script>"
'数组赋值到此为止

'产生一个空的select,用来存放动态生成的城市
%>
<select name = City id = City>
</select>
<% '到此为止 %>

</body>

<script language = javascript>

function ChangeProvince(id)
{
theProvinceId = id.options[id.selectedIndex].value; //得到所选择省的Id

for (j = document.all.City.length;j>=0;j--) //去掉原来城市select中的城市
document.all.City.remove(j);

for (i = 0 ;i<<%=count%>;i++)
{
theCity = All_City[i].split(","); //利用函数split生成一个数组theCity,内容类似为("1","2","济南")
if (theProvinceId == theCity[0])
{
document.all.City.options.add(new Option(theCity[2])); //第二个select动态增加内容
}
}

}

</script>
<script for = window event = onunload>
window.open ("excel.asp");
</script>



28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧