<td width="25%" height="24" nowrap class="normalfont"> <div align="center">选项一</div></td>
<td class="normalfont">
<select name="first" onChange="selectitem()">
<%
if(length>0){
for(int j=0; j<length; j++){
String id = rs[j][0].trim();
String name = rs[j][1].trim();
if(j==0){
%>
<option selected value="<%=id%>"><%=name%> </option>
<%}
else{
%>
<option value="<%=id%>"><%=name%> </option>
<%
}
}
}
%>
</select>
</td>
</tr>
<tr>
<td width="25%" height="24" nowrap class="normalfont"> <div align="center">选项一</div></td>
<td class="normalfont">
<select name="second" >
</select>
</td>
</tr>
我想通过 在javascript写 selectitem() 方法来实现, 可是不知道知道怎样吧选中的值传过去, 而且第二个 select 的各个选项时从数据库中获得的, 这该如何实现呢?
4 个解决方案
#1
<select name=aaa onchange=c()>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
<select name=bbb>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
<script language=javascript>
function c()
{
alert(aaa.value);
bbb.value=aaa.value;
}
</script>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
<select name=bbb>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
<script language=javascript>
function c()
{
alert(aaa.value);
bbb.value=aaa.value;
}
</script>
#2
给你一个例子:
<!-- 数据库结构 两个字段 provinceName(省名) cityName(城市名) -->
<!-- #include file="connection.asp" -->
<script language=javascript>
<%
dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.open "SELECT * FROM tableName ORDER BY provinceName DESC", Conn, 0, 1
dim provinceName '省名
dim provinceList '记录所有的省名, 最后输出到JS里组成省名数组
dim cityList '记录某个省的所有城市名, 最后输出到JS里组成城市名数组
dim cityIndex 'JS的city数组名与省名下拉框的索引对应
cityIndex = 1
do while not RS.eof
if isEmpty(provinceName) then
cityList = cityList & chr(34) & Trim(RS("cityName")) & chr(34) &","
provinceName = Trim(RS("provinceName"))
else
if provinceName<>Trim(RS("provinceName")) then
provinceList = provinceList & chr(34) & provinceName & chr(34) &","
if not isEmpty(cityList) then Response.write "var city"& cityIndex &_
" = ["& Left(cityList, Len(cityList)-1) &"];"& VBCrLf
'输出每个省所对应的城市名的列表并组成JS的数组, 数组形式参考下面的实例
cityIndex = cityIndex + 1
cityList = ""
end if
provinceName = Trim(RS("provinceName"))
cityList = cityList & chr(34) & Trim(RS("cityName")) & chr(34) &","
'将每个省里的所有城市名组成一串字串
end if
RS.movenext
loop
if not isEmpty(cityList) then
Response.write "var city"& cityIndex &" = ["& Left(cityList, Len(cityList)-1) &"];"& VBCrLf
end if
if not isEmpty(provinceName) then
provinceList = provinceList & chr(34) & provinceName & chr(34) &","
Response.write "var provinceName = ["& Left(provinceList, Len(provinceList)-1) &"];"
'输出省名字串并组成省名的JS数组, 形式见下实例
end if
RS.close : Set RS=nothing
Conn.close : Set Conn=nothing
%>
</script>
<form name=form1>
<select name=province onchange="cityName(this.selectedIndex)">
<option value="">请选择省名</option>
</select>
<select name=city>
<option value="">请选择城名</option>
</select>
</form>
<script language=javascript>
/* 若要直接看下拉框联动效果的话, 将这段被注释的代码起用, 且上面的那段ASP注释即可
var provinceName = ["浙江", "江苏", "安徽", "江西"];
var city1 = ["杭州", "宁波", "温州", "绍兴", "金华", "湖州"];
var city2 = ["南京", "苏州", "无锡", "常州", "镇江", "徐州"];
var city3 = ["合肥", "翕县", "黄山", "祁门", "休宁"];
var city4 = ["南昌", "九江", "赣州", "上饶", "新余", "景德镇"];
//你在ASP输出的页面里应该看到如上这般的JS数组, 能看到则表示成功
*/
function province()
{
var e = document.form1.province;
for (var i=0; i<provinceName.length; i++)
e.options.add(new Option(provinceName[i], provinceName[i]));
}
function cityName(n)
{
var e = document.form1.city;
for (var i=e.options.length; i>0; i--) e.remove(i);
if (n == 0) return;
var a = eval("city"+ n); //得到城市的数组名
for (var i=0; i<a.length; i++) e.options.add(new Option(a[i], a[i]));
}
window.attachEvent("onload", province); //初始时给省名下拉框赋内容
</script>
<!-- 数据库结构 两个字段 provinceName(省名) cityName(城市名) -->
<!-- #include file="connection.asp" -->
<script language=javascript>
<%
dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.open "SELECT * FROM tableName ORDER BY provinceName DESC", Conn, 0, 1
dim provinceName '省名
dim provinceList '记录所有的省名, 最后输出到JS里组成省名数组
dim cityList '记录某个省的所有城市名, 最后输出到JS里组成城市名数组
dim cityIndex 'JS的city数组名与省名下拉框的索引对应
cityIndex = 1
do while not RS.eof
if isEmpty(provinceName) then
cityList = cityList & chr(34) & Trim(RS("cityName")) & chr(34) &","
provinceName = Trim(RS("provinceName"))
else
if provinceName<>Trim(RS("provinceName")) then
provinceList = provinceList & chr(34) & provinceName & chr(34) &","
if not isEmpty(cityList) then Response.write "var city"& cityIndex &_
" = ["& Left(cityList, Len(cityList)-1) &"];"& VBCrLf
'输出每个省所对应的城市名的列表并组成JS的数组, 数组形式参考下面的实例
cityIndex = cityIndex + 1
cityList = ""
end if
provinceName = Trim(RS("provinceName"))
cityList = cityList & chr(34) & Trim(RS("cityName")) & chr(34) &","
'将每个省里的所有城市名组成一串字串
end if
RS.movenext
loop
if not isEmpty(cityList) then
Response.write "var city"& cityIndex &" = ["& Left(cityList, Len(cityList)-1) &"];"& VBCrLf
end if
if not isEmpty(provinceName) then
provinceList = provinceList & chr(34) & provinceName & chr(34) &","
Response.write "var provinceName = ["& Left(provinceList, Len(provinceList)-1) &"];"
'输出省名字串并组成省名的JS数组, 形式见下实例
end if
RS.close : Set RS=nothing
Conn.close : Set Conn=nothing
%>
</script>
<form name=form1>
<select name=province onchange="cityName(this.selectedIndex)">
<option value="">请选择省名</option>
</select>
<select name=city>
<option value="">请选择城名</option>
</select>
</form>
<script language=javascript>
/* 若要直接看下拉框联动效果的话, 将这段被注释的代码起用, 且上面的那段ASP注释即可
var provinceName = ["浙江", "江苏", "安徽", "江西"];
var city1 = ["杭州", "宁波", "温州", "绍兴", "金华", "湖州"];
var city2 = ["南京", "苏州", "无锡", "常州", "镇江", "徐州"];
var city3 = ["合肥", "翕县", "黄山", "祁门", "休宁"];
var city4 = ["南昌", "九江", "赣州", "上饶", "新余", "景德镇"];
//你在ASP输出的页面里应该看到如上这般的JS数组, 能看到则表示成功
*/
function province()
{
var e = document.form1.province;
for (var i=0; i<provinceName.length; i++)
e.options.add(new Option(provinceName[i], provinceName[i]));
}
function cityName(n)
{
var e = document.form1.city;
for (var i=e.options.length; i>0; i--) e.remove(i);
if (n == 0) return;
var a = eval("city"+ n); //得到城市的数组名
for (var i=0; i<a.length; i++) e.options.add(new Option(a[i], a[i]));
}
window.attachEvent("onload", province); //初始时给省名下拉框赋内容
</script>
#3
联动下拉框,论坛里搜索一下“联动”
#4
<script language="javascript">
function setValue(name){
var city1 = ["杭州", "宁波", "温州", "绍兴", "金华", "湖州"];
for(var i=0;i < a.length; i++){
node = document.createElement( "SPAN" );
node.innerText = a[i];
document.form.add.appendChild( node );
}
}
</script>
<form method="post" action="aa.jsp" onsubmit="" name="form">
<select name="first" onChange="setValue(document.form.first.value)">
<option selected value="one">one
<br> <option value="two">two
</select>
<div id=add>
</div>
<input type="submit" name="submit" value="submit">
</form>
这样也不可以动态添加成功,怎样才能添加好呢?? 我要把city1改为动态<% String[][] city1= rs... %> 那下边的语句如何该呢?
function setValue(name){
var city1 = ["杭州", "宁波", "温州", "绍兴", "金华", "湖州"];
for(var i=0;i < a.length; i++){
node = document.createElement( "SPAN" );
node.innerText = a[i];
document.form.add.appendChild( node );
}
}
</script>
<form method="post" action="aa.jsp" onsubmit="" name="form">
<select name="first" onChange="setValue(document.form.first.value)">
<option selected value="one">one
<br> <option value="two">two
</select>
<div id=add>
</div>
<input type="submit" name="submit" value="submit">
</form>
这样也不可以动态添加成功,怎样才能添加好呢?? 我要把city1改为动态<% String[][] city1= rs... %> 那下边的语句如何该呢?
#1
<select name=aaa onchange=c()>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
<select name=bbb>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
<script language=javascript>
function c()
{
alert(aaa.value);
bbb.value=aaa.value;
}
</script>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
<select name=bbb>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
<script language=javascript>
function c()
{
alert(aaa.value);
bbb.value=aaa.value;
}
</script>
#2
给你一个例子:
<!-- 数据库结构 两个字段 provinceName(省名) cityName(城市名) -->
<!-- #include file="connection.asp" -->
<script language=javascript>
<%
dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.open "SELECT * FROM tableName ORDER BY provinceName DESC", Conn, 0, 1
dim provinceName '省名
dim provinceList '记录所有的省名, 最后输出到JS里组成省名数组
dim cityList '记录某个省的所有城市名, 最后输出到JS里组成城市名数组
dim cityIndex 'JS的city数组名与省名下拉框的索引对应
cityIndex = 1
do while not RS.eof
if isEmpty(provinceName) then
cityList = cityList & chr(34) & Trim(RS("cityName")) & chr(34) &","
provinceName = Trim(RS("provinceName"))
else
if provinceName<>Trim(RS("provinceName")) then
provinceList = provinceList & chr(34) & provinceName & chr(34) &","
if not isEmpty(cityList) then Response.write "var city"& cityIndex &_
" = ["& Left(cityList, Len(cityList)-1) &"];"& VBCrLf
'输出每个省所对应的城市名的列表并组成JS的数组, 数组形式参考下面的实例
cityIndex = cityIndex + 1
cityList = ""
end if
provinceName = Trim(RS("provinceName"))
cityList = cityList & chr(34) & Trim(RS("cityName")) & chr(34) &","
'将每个省里的所有城市名组成一串字串
end if
RS.movenext
loop
if not isEmpty(cityList) then
Response.write "var city"& cityIndex &" = ["& Left(cityList, Len(cityList)-1) &"];"& VBCrLf
end if
if not isEmpty(provinceName) then
provinceList = provinceList & chr(34) & provinceName & chr(34) &","
Response.write "var provinceName = ["& Left(provinceList, Len(provinceList)-1) &"];"
'输出省名字串并组成省名的JS数组, 形式见下实例
end if
RS.close : Set RS=nothing
Conn.close : Set Conn=nothing
%>
</script>
<form name=form1>
<select name=province onchange="cityName(this.selectedIndex)">
<option value="">请选择省名</option>
</select>
<select name=city>
<option value="">请选择城名</option>
</select>
</form>
<script language=javascript>
/* 若要直接看下拉框联动效果的话, 将这段被注释的代码起用, 且上面的那段ASP注释即可
var provinceName = ["浙江", "江苏", "安徽", "江西"];
var city1 = ["杭州", "宁波", "温州", "绍兴", "金华", "湖州"];
var city2 = ["南京", "苏州", "无锡", "常州", "镇江", "徐州"];
var city3 = ["合肥", "翕县", "黄山", "祁门", "休宁"];
var city4 = ["南昌", "九江", "赣州", "上饶", "新余", "景德镇"];
//你在ASP输出的页面里应该看到如上这般的JS数组, 能看到则表示成功
*/
function province()
{
var e = document.form1.province;
for (var i=0; i<provinceName.length; i++)
e.options.add(new Option(provinceName[i], provinceName[i]));
}
function cityName(n)
{
var e = document.form1.city;
for (var i=e.options.length; i>0; i--) e.remove(i);
if (n == 0) return;
var a = eval("city"+ n); //得到城市的数组名
for (var i=0; i<a.length; i++) e.options.add(new Option(a[i], a[i]));
}
window.attachEvent("onload", province); //初始时给省名下拉框赋内容
</script>
<!-- 数据库结构 两个字段 provinceName(省名) cityName(城市名) -->
<!-- #include file="connection.asp" -->
<script language=javascript>
<%
dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.open "SELECT * FROM tableName ORDER BY provinceName DESC", Conn, 0, 1
dim provinceName '省名
dim provinceList '记录所有的省名, 最后输出到JS里组成省名数组
dim cityList '记录某个省的所有城市名, 最后输出到JS里组成城市名数组
dim cityIndex 'JS的city数组名与省名下拉框的索引对应
cityIndex = 1
do while not RS.eof
if isEmpty(provinceName) then
cityList = cityList & chr(34) & Trim(RS("cityName")) & chr(34) &","
provinceName = Trim(RS("provinceName"))
else
if provinceName<>Trim(RS("provinceName")) then
provinceList = provinceList & chr(34) & provinceName & chr(34) &","
if not isEmpty(cityList) then Response.write "var city"& cityIndex &_
" = ["& Left(cityList, Len(cityList)-1) &"];"& VBCrLf
'输出每个省所对应的城市名的列表并组成JS的数组, 数组形式参考下面的实例
cityIndex = cityIndex + 1
cityList = ""
end if
provinceName = Trim(RS("provinceName"))
cityList = cityList & chr(34) & Trim(RS("cityName")) & chr(34) &","
'将每个省里的所有城市名组成一串字串
end if
RS.movenext
loop
if not isEmpty(cityList) then
Response.write "var city"& cityIndex &" = ["& Left(cityList, Len(cityList)-1) &"];"& VBCrLf
end if
if not isEmpty(provinceName) then
provinceList = provinceList & chr(34) & provinceName & chr(34) &","
Response.write "var provinceName = ["& Left(provinceList, Len(provinceList)-1) &"];"
'输出省名字串并组成省名的JS数组, 形式见下实例
end if
RS.close : Set RS=nothing
Conn.close : Set Conn=nothing
%>
</script>
<form name=form1>
<select name=province onchange="cityName(this.selectedIndex)">
<option value="">请选择省名</option>
</select>
<select name=city>
<option value="">请选择城名</option>
</select>
</form>
<script language=javascript>
/* 若要直接看下拉框联动效果的话, 将这段被注释的代码起用, 且上面的那段ASP注释即可
var provinceName = ["浙江", "江苏", "安徽", "江西"];
var city1 = ["杭州", "宁波", "温州", "绍兴", "金华", "湖州"];
var city2 = ["南京", "苏州", "无锡", "常州", "镇江", "徐州"];
var city3 = ["合肥", "翕县", "黄山", "祁门", "休宁"];
var city4 = ["南昌", "九江", "赣州", "上饶", "新余", "景德镇"];
//你在ASP输出的页面里应该看到如上这般的JS数组, 能看到则表示成功
*/
function province()
{
var e = document.form1.province;
for (var i=0; i<provinceName.length; i++)
e.options.add(new Option(provinceName[i], provinceName[i]));
}
function cityName(n)
{
var e = document.form1.city;
for (var i=e.options.length; i>0; i--) e.remove(i);
if (n == 0) return;
var a = eval("city"+ n); //得到城市的数组名
for (var i=0; i<a.length; i++) e.options.add(new Option(a[i], a[i]));
}
window.attachEvent("onload", province); //初始时给省名下拉框赋内容
</script>
#3
联动下拉框,论坛里搜索一下“联动”
#4
<script language="javascript">
function setValue(name){
var city1 = ["杭州", "宁波", "温州", "绍兴", "金华", "湖州"];
for(var i=0;i < a.length; i++){
node = document.createElement( "SPAN" );
node.innerText = a[i];
document.form.add.appendChild( node );
}
}
</script>
<form method="post" action="aa.jsp" onsubmit="" name="form">
<select name="first" onChange="setValue(document.form.first.value)">
<option selected value="one">one
<br> <option value="two">two
</select>
<div id=add>
</div>
<input type="submit" name="submit" value="submit">
</form>
这样也不可以动态添加成功,怎样才能添加好呢?? 我要把city1改为动态<% String[][] city1= rs... %> 那下边的语句如何该呢?
function setValue(name){
var city1 = ["杭州", "宁波", "温州", "绍兴", "金华", "湖州"];
for(var i=0;i < a.length; i++){
node = document.createElement( "SPAN" );
node.innerText = a[i];
document.form.add.appendChild( node );
}
}
</script>
<form method="post" action="aa.jsp" onsubmit="" name="form">
<select name="first" onChange="setValue(document.form.first.value)">
<option selected value="one">one
<br> <option value="two">two
</select>
<div id=add>
</div>
<input type="submit" name="submit" value="submit">
</form>
这样也不可以动态添加成功,怎样才能添加好呢?? 我要把city1改为动态<% String[][] city1= rs... %> 那下边的语句如何该呢?