|
看我篇文章:
一个asp+javascript结合的二级联动的例子
<%
dim rs
dim sql
dim count
set rs=server.createobject("adodb.recordset")
sql = "select * from Nclass order by Nclassid asc"
rs.open sql,conn,1,1'打开分类
%><SCRIPT language="JavaScript">
var onecount;
onecount=0;
subcat = new Array();//用JS建一个数组
<%
count = 0
do while not rs.eof '通过ASP循环来生成这个数组
%>
subcat[<%=count%>] = new Array("<%= trim(rs("Nclass"))%>","<%= trim(rs("classid"))%>","<%= trim(rs("Nclassid"))%>");//得到SUBCAT[I]=new Array(NCLASS,CLASSID,NCLASSID)格式的数组
<%
count = count + 1
rs.movenext
loop
rs.close
%>
onecount=<%=count%>;
function changelocation(locationid)
{
document.myform.Nclassid.length = 0; //副SELECT的SELECT的个数为0
var locationid=locationid;//声明一下
var i;
for (i=0;i < onecount; i++)//把数组顺次循环出来
{
if (subcat[i][1] == locationid)//如果上面数组中有一个是现在的LOCATIONID,也就是主SELECT改变时,document.myform.classid.options[document.myform.classid.selectedIndex].value这个值,就执行下面的
{
document.myform.Nclassid.options[document.myform.Nclassid.length] = new Option(subcat[i][0], subcat[i][2]);//生成副SELECT的OPTION,document.myform.Nclassid.length,有多少个符合的数组,长度就有多少,把数组值传给OPTIONS,当到哪个相同数组时,就生成第几个OPTION,并把数组值传给OPTION
}
}
}
</SCRIPT>
主select
<SELECT name="classid" onChange="changelocation(document.myform.classid.options[document.myform.classid.selectedIndex].value)" size="1">
本文来源于网络小筑 http://www.iwebtrados.com.cn/ , 原文地址:http://www.iwebtrados.com.cn/post/11.html |
|