一个简单的问题,请高手指点啊!

yuanjian0211 2004-08-24 10:45:56
有两个下拉列表,比如一个是选省份的,另一个是相应省份的地市的,也就是说,如果我在第一个下拉列表里选择了陕西省,那么第二个下拉列表里只能显示陕西省的地市,请问这个是怎么实现的,谢谢了,很急!
...全文
153 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanjian0211 2004-08-26
  • 打赏
  • 举报
回复
唉 谢谢各位了!
BillSmiph 2004-08-25
  • 打赏
  • 举报
回复
请参考简单的范例:

<FORM name="isc">
<table border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td nowrap height="11">

<select name="example" size="1" onChange="redirect(this.options.selectedIndex)">
<option selected>---Select1-------------</option>
<option>Webmaster Sites</option>
<option>News Sites</option>
</select>

<select name="stage2" size="1" onChange="redirect1(this.options.selectedIndex)">
<option value=" " selected> </option>
<option value=" " selected>---Select2--------------</option>
<option value=" " selected>---Select2--------------</option>
</select>

<select name="stage3" size="1" onChange="redirect2(this.options.selectedIndex)">
<option value=" " selected> </option>
<option value=" " selected>---Select3----------------</option>
<option value=" " selected>---Select3----------------</option>
</select>

<script>
<!--

/*
Triple Combo Script Credit
By Hamid Cheheltani/ Website Abstraction (http://www.wsabstract.com)
Visit <A HREF="http://wsabstract.com" TARGET=_blank>http://wsabstract.com</A> for this and over 400+ other scripts
*/

var groups=document.isc.example.options.length
var group=new Array(groups)
for (i=0; i<groups; i++)
group[i]=new Array()

group[0][0]=new Option("---Select2---"," ");

group[1][0]=new Option("Now Select This One"," ");
group[1][1]=new Option("JavaScript","47");
group[1][2]=new Option("DHTML","46");
group[1][3]=new Option("CGI","45");

group[2][0]=new Option("Now Select This One"," ");
group[2][1]=new Option("General News","115");
group[2][2]=new Option("Technology News","116");

var temp=document.isc.stage2


function redirect(x){
for (m=temp.options.length-1;m>0;m--)
temp.options[m]=null
for (i=0;i<group[x].length;i++){
temp.options[i]=new Option(group[x][i].text,group[x][i].value)
}
temp.options[0].selected=true
redirect1(0)
}

var secondGroups=document.isc.stage2.options.length
var secondGroup=new Array(groups)
for (i=0; i<groups; i++) {
secondGroup[i]=new Array(group[i].length)
for (j=0; j<group[i].length; j++) {
secondGroup[i][j]=new Array() }}

secondGroup[0][0][0]=new Option("---Select 3---"," ");
secondGroup[1][0][0]=new Option("---Select 3---"," ");
secondGroup[1][1][0]=new Option("Now Select This One"," ");
secondGroup[1][1][1]=new Option("Website Abstraction","http://wsabstract.com");
secondGroup[1][1][2]=new Option("JavaScript for the non programmer","http://webteacher.com/javascript/");
secondGroup[1][1][3]=new Option("Java-Scripts.net","http://java-scripts.net");

secondGroup[1][2][0]=new Option("Now Select This One"," ");
secondGroup[1][2][1]=new Option("Dynamic Drive","http://www.dynamicdrive.com");
secondGroup[1][2][2]=new Option("Beginner\'s Guide to DHTML","http://www.geocities.com/ResearchTriangle/Facility/4490/");
secondGroup[1][2][3]=new Option("Web Coder","http://webcoder.com/");

secondGroup[1][3][0]=new Option("Now Select This One"," ");
secondGroup[1][3][1]=new Option("CGI Resources","http://www.cgi-resources.com");
secondGroup[1][3][2]=new Option("Ada\'s Intro to CGI","http://adashimar.hypermart.net/");

secondGroup[2][0][0]=new Option("---Select 3---"," ");
secondGroup[2][1][0]=new Option("Now Select This One"," ");
secondGroup[2][1][1]=new Option("CNN","http://www.cnn.com");
secondGroup[2][1][2]=new Option("MSNBC","http://www.msnbc.com");
secondGroup[2][1][3]=new Option("ABC News","http://www.abcnews.com");

secondGroup[2][2][0]=new Option("Now Select A Page"," ");
secondGroup[2][2][1]=new Option("News.com","http://www.news.com");
secondGroup[2][2][2]=new Option("Wired","http://www.wired.com");

var temp1=document.isc.stage3
function redirect1(y){
for (m=temp1.options.length-1;m>0;m--)
temp1.options[m]=null
for (i=0;i<secondGroup[document.isc.example.options.selectedIndex][y].length;i++){
temp1.options[i]=new Option(secondGroup[document.isc.example.options.selectedIndex][y][i].text,secondGroup[document.isc.example.options.selectedIndex][y][i].value)
}
temp1.options[0].selected=true
}

function redirect2(z){
window.location=temp1[z].value
}

//-->
</script>

</td>
</tr>
</table>
</FORM>
miqier 2004-08-25
  • 打赏
  • 举报
回复
我是用asp从数据库中读取记录,动态生成一个JS数组,然后在页面跳转之前调用时就相当于是调用静态js,感觉速度还行。
-----------------------------------
生成JS列表的asp页面 CreateCity.asp
-----------------------------------
<!-- #include file="conn.asp" -->
<%
sql = "select * from Area_List Where Levels = 0"
Set rsList = Server.CreateObject("Adodb.RecordSet")
rsList.Open sql,conn,3

Response.Write "tCitys = new Array; "&chr(13)
Response.Write "var ProvinceArray = new Array;"&chr(13)
Response.Write "ProvinceArray[0]="&chr(34)&"省份"&chr(34)&chr(13)
For i = 1 to rsList.RecordCount
Response.Write "ProvinceArray["&i&"]="&chr(34)&rsList("cat_name")&chr(34)&";"&chr(13)

sql = "select * from Area_List Where Levels = 1 and parentid = "&rsList("ID")
set rsChild = Server.CreateObject("Adodb.RecordSet")
rsChild.Open sql,conn,3

Response.Write "tCitys["&i&"] = new Array();"&chr(13)
For j = 0 to rsChild.RecordCount - 1
Response.Write "tCitys["&i&"]["&j&"]="&chr(34)&rsChild("cat_name")&chr(34)&";"&chr(13)
rsChild.MoveNext
If rsChild.EOF or rsChild.BOF Then
Exit For
End If
Next

rsList.MoveNext
If rsList.EOF or rsList.BOF Then
Exit For
End If
Next
%>

------------------------------------
实现关联下拉框的JS文件 selectcity.js
------------------------------------

function ProvinceOptionMenu()
{
var i;
provincebox = document.formbill.loc_city;
for(i = 0; i < ProvinceArray.length; i++)
{
provincebox.options[i] = new Option(ProvinceArray[i],ProvinceArray[i]);
}
provincebox.length = i;
}



function selectcity()
{
provincebox = document.formbill.loc_city;
selcity = parseInt(provincebox.selectedIndex);
tCity = tCitys[selcity];
citybox = document.formbill.loc_id;
if(tCity != null)
{
citybox = document.formbill.loc_id;
if (tCity.length>1){
citybox.options[0] = new Option("请选择",-1);
for(i = 0; i < tCity.length; i++)
{
str = tCity[i];
citybox.options[i+1] = new Option(str, str);
}
citybox.length = i+1;
}
else
{
str = tCity[0];
citybox.options[0] = new Option(str,str);
citybox.length=1;
citybox.options[0].selected;
}



}
else{
if (citybox != null){
citybox.options[0] = new Option("请先选择省份",-1);
citybox.length = 1;}
}
}

-----------------------------------------------
显示页面 City.asp
-----------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
.fs12 {
font-size: 12px;
color: #000000;
text-decoration: none;
line-height: 150%;
}
.boxbg {
background-image: url(../images/box_rightbof_bg.gif);
background-repeat: no-repeat;
background-position: right bottom;
}
a:link {
font-size: 12px;
color: #000000;
text-decoration: none;
}
a:visited {
font-size: 12px;
color: #000000;
text-decoration: none;
}
a:hover {
font-size: 12px;
color: #000000;
text-decoration: underline;
}
-->
</style>
<link rel="stylesheet" type="text/css" href="styles.css">
<script language=javascript src="CreateCity.asp"></script>
<script language=javascript src="selectcity.js"></script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<title></title>
<table width="766" border=0 cellpadding=2 cellspacing=0 align="center">
<tr>
<td width="178" valign="top" background="../images/body_left_bg.gif" align="center">
</td>
<td width="7"><img src="../images/sp_temp.gif" width="7" height="30"></td>
<td width="587" valign="top" background="../images/body_main_bg.gif">
<table width="100%" border="1">
<tr>
<td>
</td>
<td valign="top">
<table width="80%" border="0" cellspacing="1" cellpadding="3" bgcolor="#f0f0f0">
<form action="cat_setting.asp" method="post" name="formbill" id="formbill">
<tr>
<td><b>城市列表:</b></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">

  <select NAME="loc_city" size="1" onChange="javascript:selectcity()" class="font">
<option value=-1 selected>省份</option>
</SELECT>
<script language="javascript">
ProvinceOptionMenu();
</script>
<SELECT NAME="loc_id" class="font">
<option value=-1 selected>请先选择省份</option>
</SELECT>
<br>
新值:
<input type="text" name="cat_name" id="cat_name">

</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
lg9706032921 2004-08-25
  • 打赏
  • 举报
回复
用两个表吧,这样好点,可以随时改数据,再就用楼上的那个change函数就行了
shqlsl 2004-08-25
  • 打赏
  • 举报
回复
有2种方法. 简单的用js生成数组  复杂用xmlhttp
简单的例子:
<script>
var ss = new Array();
ss[0] = ['杭州','温州','宁波'];
ss[1] = ['福州','泉州','漳州'];
ss[2] = ['广州','揭阳','深圳'];
ss[3] = [];
</script>
<script>
function change(x){
s=ss[x]
test.length=0;
for(var i=0;i<s.length;i++)
{
test.options[i]=new Option(s[i],s[i],false,false)
}
}
</script>
<select onchange=change(selectedIndex)>
<option>浙江</option>
<option>福建</option>
<option>广东</option>
<option>未知</option>
</select>
<select id="test">
</select>
flying310 2004-08-25
  • 打赏
  • 举报
回复
这就是一个二级联动菜单的问题。很容易可以参照网上的联动菜单的例子很多。
wfwclyms 2004-08-25
  • 打赏
  • 举报
回复
楼上的就是了
makay 2004-08-25
  • 打赏
  • 举报
回复
楼上的都说了。
主要是onChange事件。
wittyWYZ 2004-08-25
  • 打赏
  • 举报
回复
可以用xml实现和js实现.
bearzhan88 2004-08-25
  • 打赏
  • 举报
回复
http://www.21-sun.com/career/default.asp
这个是偶的
bearzhan88 2004-08-25
  • 打赏
  • 举报
回复
想要动态的 就要自己建立个数据库,想要静态的就要写N行赋值,自己选择了。
wsdzmhongm 2004-08-24
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/2628/2628045.xml?temp=.12875

28,391

社区成员

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

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