两个<select>之间如何联动?(50分)

  • 主题发起人 greatwzq
  • 开始时间
G

greatwzq

Unregistered / Unconfirmed
GUEST, unregistred user!
用ASP编写一个用户登陆注册网页,其中要填写的东西有国家、城市,想利用两个select控
件实现,国家从select1列表里面选择,当用户选择完国家后希望在select2里面列出该国
家的城市供选择。国家和城市列表都是后台通过数据库调用动态改变。
现在的问题是两个select如何协调?不通过提交表单怎么实现?因为表单上还有其它东西,
一提交再返回就没有了。
 
那你的select列表改变时没有事件吗?
 
ASP?没写过。但我想你选择列表时,应该有相应的事件,通过该事件,
应该能够解决你的问题。
 
用JAVASCRIPT来做
不复杂:)
 
看看下面的代码
hyj1.asp
再自已想想就OK了

<head>
<script>
function checkd1(vard1)
{document.location="hyj1.asp?d3="+vard1;}
</script>
</head>
<body>
<table border="1" width="550" height="20" cellspacing="0" bordercolor="#3399FF">
<tr height="36" align="center">
<form name="db" action="hyj1.asp" method="GET">
<td width="30">
<select size="1" name="D3" onchange="checkd1(this.options[this.selectedIndex].value)" ;>
response.write"<option value='2003' <%if request.querystring("d3")="2003" then
response.write "selected" end if%>>2003</option>"
response.write"<option value='2002' <%if request.querystring("d3")="2002" then
response.write "selected" end if%>>2002</option>"
</select>
</td>
<td width="30">
<select size="1" name="D1" onchange="checkd2(this.options[this.selectedIndex].value)" ;>
<%if request.querystring("d3")="2003" then
%>
<option value='2003'>二零零三年</option>
<%else
%>
<option value='2002'>二零零二年</option>
<%end if%>
</td>
</from>
</select>
</tr>
</table>
</body>
 
没看清你说还要调用数据库
改成这样
<head>
<script>
function checkd1(vard1)
{document.location="hyj1.asp?d3="+vard1;}
</script>
</head>
<body>
<table border="1" width="550" height="20" cellspacing="0" bordercolor="#3399FF">
<tr height="36" align="center">
<form name="db" action="hyj1.asp" method="GET">
<td width="30">
<select size="1" name="D3" onchange="checkd1(this.options[this.selectedIndex].value)" ;>
response.write"<option value='2003' <%if request.querystring("d3")="中国" then
response.write "selected" end if%>>2003</option>"
response.write"<option value='2002' <%if request.querystring("d3")="美国" then
response.write "selected" end if%>>2002</option>"
</select>
<%
call query2list("select 城市名 from statotidx where 国家=request.querystring("d3") order by 排序","城市名")
sub query2list(parmquery, listname)
Set conntemp = Server.CreateObject("ADODB.Connection")
conntemp.open "DNSNAME","DNS","DNSpwd"
set rstemp=conntemp.execute(parmquery)
%>
<p><select name="d1" size="1">
<%do while not rstemp.eof%> <option value="<%=rstemp(1)%>" <%if request.querystring("d1")=rstemp(1) then
response.write "selected"
end if%>><%=RStemp(1)%></option>
<% rstemp.movenext
loop
conntemp.close
end sub%>
</select>
</tr>
</from>
</table>
</body>
 
问题解决后记得给分给我哟
我ASP还可以,但delphi还很菜,我要积点分到delphi版去提问
 
写个onchange事件
 
解决办法有两个,
1。使用javaScript,如果你的数据量不是特别大,你可以一次把所有的纪录检索出,放
在一个(或两个)别人看不见的combox里,比如下面(value/Text)
Los/USA, Washington/USA ,Beijing/China, Shanghai/China ....
同时把所有的国家写到一个combox里, 在他的onChange事件里,先取回自己的
国家值(比如China),然后遍历一边上面的combox,把所有属于china的值动
态加载到城市这个框里。这个方法简单,不用每次提交,但是如果数据量太大,
不推荐使用。具体代码可以查我回答过的问题
2。把你的城市的下拉框放在一个iframe里,这样当国家改变时,只要提交这个ifame
就可以了,不会影响整个画面,缺点,iframe只能在ie里使用,netscape不行
 
<!--#include file="link/sqlcn.asp"-->
<HTML>
<HEAD>
<%


function d(id)
dim f
set ca=server.CreateObject("adodb.recordset")
ca.open"select * from city where provinceid='"&amp;
id &amp;"'",sqlce,1,1
do
while not ca.eof
if f="" then
f=trim(ca("cityname"))
else
f=f&amp;"','"&amp;trim(ca("cityname"))
end if
ca.movenext
loop
d=f
end function
function u(id)
dim x
set co=server.CreateObject("adodb.recordset")
co.open"select * from city where provinceid='"&amp;
id &amp;"'",sqlce,1,1
do
while not co.eof
if x="" then
x=trim(co("cityid"))
else
x=x&amp;"','"&amp;trim(co("cityid"))
end if
co.movenext
loop
u=x
end function

function c
set ce=server.createobject("adodb.recordset")
ce.open"select provinceid from province",sqlce,1,1
dim m
do
while not ce.eof
m=m&amp;"case '"&amp;trim(ce("provinceid"))&amp;"':"&amp;vbCrlf&amp;_
"var labels = new Array('"&amp;
d(trim(ce("provinceid"))) &amp;"');"&amp;vbCrlf&amp;_
"var values = new Array('"&amp;
u(trim(ce("provinceid"))) &amp;"');"&amp;vbCrlf&amp;_
"break;"&amp;vbCrlf
ce.movenext
loop
c=m
end function
%>
<%response.Write("<SCRIPT LANGUAGE=JavaScript>"&amp;vbCrlf&amp;_
"function setcity() {"&amp;vbCrlf&amp;_
"switch (document.shengshi.sheng.value) {"&amp;vbCrlf&amp;_
c&amp;_
"}"&amp;vbCrlf&amp;_
"document.shengshi.city.options.length = 0;"&amp;vbCrlf&amp;_
"for(var i = 0;
i < labels.length;
i++) {"&amp;vbCrlf&amp;_
"document.shengshi.city.add(document.createElement('OPTION'));"&amp;vbCrlf&amp;_
"document.shengshi.city.options.text=labels;"&amp;vbCrlf&amp;_
"document.shengshi.city.options.value=values;"&amp;vbCrlf&amp;_
"}"&amp;vbCrlf&amp;_
"document.shengshi.city.selectedIndex = 0;"&amp;vbCrlf&amp;_
"}"&amp;vbCrlf&amp;_
"</SCRIPT>")
%>
</HEAD>
<BODY>
<FORM NAME="shengshi" action="a.asp" method="post">
省:
<SELECT NAME="sheng" OnChange="setcity()">
<% set cdm=server.createobject("adodb.recordset")
cdm.open"select * from province",sqlce,1,1
do
while not cdm.eof %>
<OPTION value=<% =trim(cdm("provinceid")) %>><% =trim(cdm("provincename")) %></OPTION>
<% cdm.movenext
loop

%>
</SELECT>
市:
<SELECT NAME="city">
</SELECT>
<input type=submit>
</FORM>
<script language="JavaScript">
<!--
setcity()
-->
</script>

</BODY>
</HTML>

呵呵,一个同事弄出来的,你自己回去慢慢看吧,反正是可以用了
 
不是我能力所在范围
呵!!
 
顶部