求一段代码,是关于从数据库里读取数据到三级联动下拉列表的

时间:2022-09-03 10:19:22
在网上看了一些关于asp+js读取数据库的三级联动代码,但是大部分都是数据库里为三个表

我的数据库里只有一个表,字段分别是 id 一级类别 二级类别 三级类别 ,这样的三级联动应该怎么写呢,尝试了几次都失败了,特求一段代码


功用:在提交表单里用三级联动选择出对应的值,写到数据库另一表里。

4 个解决方案

#1


从数据库中取相应的数据,需要关联的查询。
建议采用页面直接加载代码。
有两种方法,
1。网上搜省市三级联动 js的文件,再在相应的网页里引用。
2。代码直接复制,如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>省市县关联菜单</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Generator" content="EditPlus">
<meta name="Author" content="fason,阿信,蒲佛信">
<meta name="Keywords" content="下拉菜单,关联,多级,fason,阿信,蒲佛信">
<meta name="Description" content="此程序为简单的省市县关联下拉菜单,有问题请往fason_pfx@hotmail.com发信">
<style>
body,select
{
font-size:9pt;
font-family:Verdana;
}
a
{
color:red;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Dsy()
{
this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}

function change(v){
var str="0";
for(i=0;i<v;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
var ss=document.getElementById(s[v]);
with(ss){
length = 0;
options[0]=new Option(opt0[v],opt0[v]);
if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
{
if(dsy.Exists(str)){
ar = dsy.Items[str];
for(i=0;i<ar.length;i++)options[length]=new Option(ar[i],ar[i]);
if(v)options[1].selected = true;
}
}
if(++v<s.length){change(v);}
}
}

var dsy = new Dsy();

dsy.add("0",["安徽","北京","福建","甘肃"]);

dsy.add("0_0",["安庆","蚌埠","巢湖","池州","滁州","阜阳","合肥","淮北","淮南","黄山","六安","马鞍山","宿州","铜陵","芜湖","宣城","亳州"]);
dsy.add("0_0_0",["安庆市","怀宁县","潜山县","宿松县","太湖县","桐城市","望江县","岳西县","枞阳县"]);
dsy.add("0_0_1",["蚌埠市","固镇县","怀远县","五河县"]);
dsy.add("0_0_2",["巢湖市","含山县","和县","庐江县","无为县"]);
dsy.add("0_0_3",["池州市","东至县","青阳县","石台县"]);
dsy.add("0_0_4",["滁州市","定远县","凤阳县","来安县","明光市","全椒县","天长市"]);
dsy.add("0_0_5",["阜南县","阜阳市","界首市","临泉县","太和县","颖上县"]);
dsy.add("0_0_6",["长丰县","肥东县","肥西县"]);
dsy.add("0_0_7",["淮北市","濉溪县"]);
dsy.add("0_0_8",["凤台县","淮南市"]);
dsy.add("0_0_9",["黄山市","祁门县","休宁县","歙县","黟县"]);
dsy.add("0_0_10",["霍邱县","霍山县","金寨县","六安市","寿县","舒城县"]);
dsy.add("0_0_11",["当涂县","马鞍山市"]);
dsy.add("0_0_12",["灵璧县","宿州市","萧县","泗县","砀山县"]);
dsy.add("0_0_13",["铜陵市","铜陵县"]);
dsy.add("0_0_14",["繁昌县","南陵县","芜湖市","芜湖县"]);
dsy.add("0_0_15",["广德县","绩溪县","郎溪县","宁国市","宣城市","泾县","旌德县"]);
dsy.add("0_0_16",["利辛县","蒙城县","涡阳县","亳州市"]);

dsy.add("0_1",["北京"]);
dsy.add("0_1_0",["北京市","密云县","延庆县"]);

dsy.add("0_2",["福州","龙岩","南平","宁德","莆田","泉州","三明","厦门","漳州"]);
dsy.add("0_2_0",["长乐市","福清市","福州市","连江县","罗源县","闽侯县","闽清县","平潭县","永泰县"]);
dsy.add("0_2_1",["长汀县","连城县","龙岩市","上杭县","武平县","永定县","漳平市"]);
dsy.add("0_2_2",["光泽县","建阳市","建瓯市","南平市","浦城县","邵武市","顺昌县","松溪县","武夷山市","政和县"]);
dsy.add("0_2_3",["福安市","福鼎市","古田县","宁德市","屏南县","寿宁县","霞浦县","周宁县","柘荣县"]);
dsy.add("0_2_4",["莆田市","仙游县"]);
dsy.add("0_2_5",["安溪县","德化县","惠安县","金门县","晋江市","南安市","泉州市","石狮市","永春县"]);
dsy.add("0_2_6",["大田县","建宁县","将乐县","明溪县","宁化县","清流县","三明市","沙县","泰宁县","永安市","尤溪县"]);
dsy.add("0_2_7",["厦门市"]);
dsy.add("0_2_8",["长泰县","东山县","华安县","龙海市","南靖县","平和县","云霄县","漳浦县","漳州市","诏安县"]);

dsy.add("0_3",["白银","定西","甘南藏族自治州","嘉峪关","金昌","酒泉","兰州","临夏回族自治州","陇南","平凉","庆阳","天水","武威","张掖"]);
dsy.add("0_3_0",["白银市","会宁县","景泰县","靖远县"]);
dsy.add("0_3_1",["定西县","临洮县","陇西县","通渭县","渭源县","漳县","岷县"]);
dsy.add("0_3_2",["迭部县","合作市","临潭县","碌曲县","玛曲县","夏河县","舟曲县","卓尼县"]);
dsy.add("0_3_3",["嘉峪关市"]);
dsy.add("0_3_4",["金昌市","永昌县"]);
dsy.add("0_3_5",["阿克塞哈萨克族自治县","安西县","敦煌市","金塔县","酒泉市","肃北蒙古族自治县","玉门市"]);
dsy.add("0_3_6",["皋兰县","兰州市","永登县","榆中县"]);
dsy.add("0_3_7",["东乡族自治县","广河县","和政县","积石山保安族东乡族撒拉族自治县","康乐县","临夏市","临夏县","永靖县"]);
dsy.add("0_3_8",["成县","徽县","康县","礼县","两当县","文县","武都县","西和县","宕昌县"]);
dsy.add("0_3_9",["崇信县","华亭县","静宁县","灵台县","平凉市","庄浪县","泾川县"]);
dsy.add("0_3_10",["合水县","华池县","环县","宁县","庆城县","庆阳市","镇原县","正宁县"]);
dsy.add("0_3_11",["甘谷县","秦安县","清水县","天水市","武山县","张家川回族自治县"]);
dsy.add("0_3_12",["古浪县","民勤县","天祝藏族自治县","武威市"]);
dsy.add("0_3_13",["高台县","临泽县","民乐县","山丹县","肃南裕固族自治县","张掖市"]);

 

//-->
</SCRIPT>
<SCRIPT LANGUAGE = JavaScript>
<!--
//** Power by Fason(2004-3-11)
//** Email:fason_pfx@hotmail.com

var s=["s1","s2","s3"];
var opt0 = ["省份","地级市","市、县级市、县"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
//-->
</SCRIPT>
</head>
<body bgcolor="#E0E0E0" onload="setup()">
多级关联菜单:
<form name="frm">
<select id="s1"><option>省份</option></select>
<select id="s2"><option>地级市</option></select>
<select id="s3"><option>市、县级市、县</option></select>
</form>
</body>
</html>

#2


引用 1 楼 hefeng_aspnet 的回复:
从数据库中取相应的数据,需要关联的查询。
建议采用页面直接加载代码。
有两种方法,
1。网上搜省市三级联动 js的文件,再在相应的网页里引用。
2。代码直接复制,如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-tran……


我没有条件的,我只需要在网页没有加载完时,能够不要背景为白色。

#3


不行啊,数据都在数据库里,而且是需要随时修改的项目,不适合这种方法

#4


我给你一个解决的思路,比如有一个表,名称为:人员表
id  学校   班级    姓名
1  中学    一班    王一
2  中学    一班    孙二
3  中学    二班    张三
你现在第一级选择是学校,第二级是班级,第三级是姓名
本页面为chaxun.asp
<%
'此处加连接数据库语句省略
set conn=......
conn.open"......"
sql="select distinct 学校 from 人员表"
set rs=conn.execute(sql)%>
<form id="form1" name="form1" method="post" action="chaxun.asp">
<table width="600" border="1">
  <tr>
    <td width="179"><div align="center">学校</div></td>
    <td width="405">

        <select name="xuexiao" id="xuexiao" onchange="chaxun.asp?xx='+this.value">
<option value="">==请选择学校==</option>
<%
if not rs.eof and not rs.bof then
do while not rs.eof%>
<option value="<%=rs("学校")%>" <%if trim(rs("学校"))=trim(request("xx")) then%> selected <%end if%>><%=rs("学校")%></option>
<%rs.movenext
loop
end if%>
        </select>
  </td>
  </tr>
  <tr>
    <td><div align="center">班级</div></td>
    <td><select name="banji" id="banji" onchange="chaxun.asp?xx=<%=request("xx")%>&bj='+this.value">
<option value="">==请选择班级==</option>
<%if request("xx")<>"" then
sql="select distinct 班级 from 人员表 where 学校='"&request("xx")&"'"
set rs1=conn.execute(sql)
if not rs1.eof and not rs1.bof then
do while not rs1.eof%>
<option value="<%=rs1("班级")%>" <%if trim(rs1("班级"))=trim(request("bj")) then%> selected <%end if%>><%=rs("班级")%></option>
<%rs1.movenext
loop
end if
end if%>
        </select></td>
  </tr>
  <tr>
    <td><div align="center">姓名</div></td>
    <td><select name="name" id="name" onchange="chaxun.asp?xx=<%=request("xx")%>&bj=<%=request("bj")%>&name='+this.value">
<option value="">==请选择人员==</option>
<%if request("xx")<>"" and reqeust("bj") <> "" then
sql="select 姓名 from 人员表 where 学校='"&request("xx")&"' and 班级='"&request("bj")&"'"
set rs2=conn.execute(sql)
if not rs2.eof and not rs2.bof then
do while not rs2.eof%>
<option value="<%=rs2("姓名")%>" <%if trim(rs2("姓名"))=trim(request("name")) then%> selected <%end if%>><%=rs2("姓名")%></option>
<%rs2.movenext
loop
end if
end if%>
        </select></td>
  </tr>
</table>
  </form>
<%set conn=nothing%>
具体没有测试,你试试看,不行再问我

#1


从数据库中取相应的数据,需要关联的查询。
建议采用页面直接加载代码。
有两种方法,
1。网上搜省市三级联动 js的文件,再在相应的网页里引用。
2。代码直接复制,如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>省市县关联菜单</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="Generator" content="EditPlus">
<meta name="Author" content="fason,阿信,蒲佛信">
<meta name="Keywords" content="下拉菜单,关联,多级,fason,阿信,蒲佛信">
<meta name="Description" content="此程序为简单的省市县关联下拉菜单,有问题请往fason_pfx@hotmail.com发信">
<style>
body,select
{
font-size:9pt;
font-family:Verdana;
}
a
{
color:red;
text-decoration:none;
}
a:hover{
text-decoration:underline;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function Dsy()
{
this.Items = {};
}
Dsy.prototype.add = function(id,iArray)
{
this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id)
{
if(typeof(this.Items[id]) == "undefined") return false;
return true;
}

function change(v){
var str="0";
for(i=0;i<v;i++){ str+=("_"+(document.getElementById(s[i]).selectedIndex-1));};
var ss=document.getElementById(s[v]);
with(ss){
length = 0;
options[0]=new Option(opt0[v],opt0[v]);
if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v)
{
if(dsy.Exists(str)){
ar = dsy.Items[str];
for(i=0;i<ar.length;i++)options[length]=new Option(ar[i],ar[i]);
if(v)options[1].selected = true;
}
}
if(++v<s.length){change(v);}
}
}

var dsy = new Dsy();

dsy.add("0",["安徽","北京","福建","甘肃"]);

dsy.add("0_0",["安庆","蚌埠","巢湖","池州","滁州","阜阳","合肥","淮北","淮南","黄山","六安","马鞍山","宿州","铜陵","芜湖","宣城","亳州"]);
dsy.add("0_0_0",["安庆市","怀宁县","潜山县","宿松县","太湖县","桐城市","望江县","岳西县","枞阳县"]);
dsy.add("0_0_1",["蚌埠市","固镇县","怀远县","五河县"]);
dsy.add("0_0_2",["巢湖市","含山县","和县","庐江县","无为县"]);
dsy.add("0_0_3",["池州市","东至县","青阳县","石台县"]);
dsy.add("0_0_4",["滁州市","定远县","凤阳县","来安县","明光市","全椒县","天长市"]);
dsy.add("0_0_5",["阜南县","阜阳市","界首市","临泉县","太和县","颖上县"]);
dsy.add("0_0_6",["长丰县","肥东县","肥西县"]);
dsy.add("0_0_7",["淮北市","濉溪县"]);
dsy.add("0_0_8",["凤台县","淮南市"]);
dsy.add("0_0_9",["黄山市","祁门县","休宁县","歙县","黟县"]);
dsy.add("0_0_10",["霍邱县","霍山县","金寨县","六安市","寿县","舒城县"]);
dsy.add("0_0_11",["当涂县","马鞍山市"]);
dsy.add("0_0_12",["灵璧县","宿州市","萧县","泗县","砀山县"]);
dsy.add("0_0_13",["铜陵市","铜陵县"]);
dsy.add("0_0_14",["繁昌县","南陵县","芜湖市","芜湖县"]);
dsy.add("0_0_15",["广德县","绩溪县","郎溪县","宁国市","宣城市","泾县","旌德县"]);
dsy.add("0_0_16",["利辛县","蒙城县","涡阳县","亳州市"]);

dsy.add("0_1",["北京"]);
dsy.add("0_1_0",["北京市","密云县","延庆县"]);

dsy.add("0_2",["福州","龙岩","南平","宁德","莆田","泉州","三明","厦门","漳州"]);
dsy.add("0_2_0",["长乐市","福清市","福州市","连江县","罗源县","闽侯县","闽清县","平潭县","永泰县"]);
dsy.add("0_2_1",["长汀县","连城县","龙岩市","上杭县","武平县","永定县","漳平市"]);
dsy.add("0_2_2",["光泽县","建阳市","建瓯市","南平市","浦城县","邵武市","顺昌县","松溪县","武夷山市","政和县"]);
dsy.add("0_2_3",["福安市","福鼎市","古田县","宁德市","屏南县","寿宁县","霞浦县","周宁县","柘荣县"]);
dsy.add("0_2_4",["莆田市","仙游县"]);
dsy.add("0_2_5",["安溪县","德化县","惠安县","金门县","晋江市","南安市","泉州市","石狮市","永春县"]);
dsy.add("0_2_6",["大田县","建宁县","将乐县","明溪县","宁化县","清流县","三明市","沙县","泰宁县","永安市","尤溪县"]);
dsy.add("0_2_7",["厦门市"]);
dsy.add("0_2_8",["长泰县","东山县","华安县","龙海市","南靖县","平和县","云霄县","漳浦县","漳州市","诏安县"]);

dsy.add("0_3",["白银","定西","甘南藏族自治州","嘉峪关","金昌","酒泉","兰州","临夏回族自治州","陇南","平凉","庆阳","天水","武威","张掖"]);
dsy.add("0_3_0",["白银市","会宁县","景泰县","靖远县"]);
dsy.add("0_3_1",["定西县","临洮县","陇西县","通渭县","渭源县","漳县","岷县"]);
dsy.add("0_3_2",["迭部县","合作市","临潭县","碌曲县","玛曲县","夏河县","舟曲县","卓尼县"]);
dsy.add("0_3_3",["嘉峪关市"]);
dsy.add("0_3_4",["金昌市","永昌县"]);
dsy.add("0_3_5",["阿克塞哈萨克族自治县","安西县","敦煌市","金塔县","酒泉市","肃北蒙古族自治县","玉门市"]);
dsy.add("0_3_6",["皋兰县","兰州市","永登县","榆中县"]);
dsy.add("0_3_7",["东乡族自治县","广河县","和政县","积石山保安族东乡族撒拉族自治县","康乐县","临夏市","临夏县","永靖县"]);
dsy.add("0_3_8",["成县","徽县","康县","礼县","两当县","文县","武都县","西和县","宕昌县"]);
dsy.add("0_3_9",["崇信县","华亭县","静宁县","灵台县","平凉市","庄浪县","泾川县"]);
dsy.add("0_3_10",["合水县","华池县","环县","宁县","庆城县","庆阳市","镇原县","正宁县"]);
dsy.add("0_3_11",["甘谷县","秦安县","清水县","天水市","武山县","张家川回族自治县"]);
dsy.add("0_3_12",["古浪县","民勤县","天祝藏族自治县","武威市"]);
dsy.add("0_3_13",["高台县","临泽县","民乐县","山丹县","肃南裕固族自治县","张掖市"]);

 

//-->
</SCRIPT>
<SCRIPT LANGUAGE = JavaScript>
<!--
//** Power by Fason(2004-3-11)
//** Email:fason_pfx@hotmail.com

var s=["s1","s2","s3"];
var opt0 = ["省份","地级市","市、县级市、县"];
function setup()
{
for(i=0;i<s.length-1;i++)
document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
change(0);
}
//-->
</SCRIPT>
</head>
<body bgcolor="#E0E0E0" onload="setup()">
多级关联菜单:
<form name="frm">
<select id="s1"><option>省份</option></select>
<select id="s2"><option>地级市</option></select>
<select id="s3"><option>市、县级市、县</option></select>
</form>
</body>
</html>

#2


引用 1 楼 hefeng_aspnet 的回复:
从数据库中取相应的数据,需要关联的查询。
建议采用页面直接加载代码。
有两种方法,
1。网上搜省市三级联动 js的文件,再在相应的网页里引用。
2。代码直接复制,如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-tran……


我没有条件的,我只需要在网页没有加载完时,能够不要背景为白色。

#3


不行啊,数据都在数据库里,而且是需要随时修改的项目,不适合这种方法

#4


我给你一个解决的思路,比如有一个表,名称为:人员表
id  学校   班级    姓名
1  中学    一班    王一
2  中学    一班    孙二
3  中学    二班    张三
你现在第一级选择是学校,第二级是班级,第三级是姓名
本页面为chaxun.asp
<%
'此处加连接数据库语句省略
set conn=......
conn.open"......"
sql="select distinct 学校 from 人员表"
set rs=conn.execute(sql)%>
<form id="form1" name="form1" method="post" action="chaxun.asp">
<table width="600" border="1">
  <tr>
    <td width="179"><div align="center">学校</div></td>
    <td width="405">

        <select name="xuexiao" id="xuexiao" onchange="chaxun.asp?xx='+this.value">
<option value="">==请选择学校==</option>
<%
if not rs.eof and not rs.bof then
do while not rs.eof%>
<option value="<%=rs("学校")%>" <%if trim(rs("学校"))=trim(request("xx")) then%> selected <%end if%>><%=rs("学校")%></option>
<%rs.movenext
loop
end if%>
        </select>
  </td>
  </tr>
  <tr>
    <td><div align="center">班级</div></td>
    <td><select name="banji" id="banji" onchange="chaxun.asp?xx=<%=request("xx")%>&bj='+this.value">
<option value="">==请选择班级==</option>
<%if request("xx")<>"" then
sql="select distinct 班级 from 人员表 where 学校='"&request("xx")&"'"
set rs1=conn.execute(sql)
if not rs1.eof and not rs1.bof then
do while not rs1.eof%>
<option value="<%=rs1("班级")%>" <%if trim(rs1("班级"))=trim(request("bj")) then%> selected <%end if%>><%=rs("班级")%></option>
<%rs1.movenext
loop
end if
end if%>
        </select></td>
  </tr>
  <tr>
    <td><div align="center">姓名</div></td>
    <td><select name="name" id="name" onchange="chaxun.asp?xx=<%=request("xx")%>&bj=<%=request("bj")%>&name='+this.value">
<option value="">==请选择人员==</option>
<%if request("xx")<>"" and reqeust("bj") <> "" then
sql="select 姓名 from 人员表 where 学校='"&request("xx")&"' and 班级='"&request("bj")&"'"
set rs2=conn.execute(sql)
if not rs2.eof and not rs2.bof then
do while not rs2.eof%>
<option value="<%=rs2("姓名")%>" <%if trim(rs2("姓名"))=trim(request("name")) then%> selected <%end if%>><%=rs2("姓名")%></option>
<%rs2.movenext
loop
end if
end if%>
        </select></td>
  </tr>
</table>
  </form>
<%set conn=nothing%>
具体没有测试,你试试看,不行再问我