这段javascript主要来自于网上常用的省市联动菜单的源码,发现可以应用到很多地方,我想对大家也很有用处,所以就贴出来和大家共享,不过细节我就不说了,大家一看就会明白,废话不说了,看代码!
<%
sql = "SELECT * FROM ProvinceCityList where parent<>0"
set rs = conn.execute(sql)
%>
<script language = "JavaScript">
var onecount;
onecount=0;
subcat = new Array();
<%
count = 0
do while not rs.eof
%>
//市、省、市对应ID
subcat[<%=count%>] = new Array("<%=rs("city")%>","<%=rs("Province")%>","<%=rs("id")%>");
<%
count = count + 1
rs.movenext
loop
rs.close
set rs=nothing
%>
onecount=<%=count%>;
function changelocation(locationid)
{
document.myform.smallclass.length = 0;
var locationid=locationid;
var i;
document.myform.smallclass.options[0] = new Option('==所有城市==','');
for (i=0;i < onecount; i++)
{
if (subcat[i][1] == locationid)
{
//alert(subcat[i][1]);
document.myform.smallclass.options[document.myform.smallclass.length] = new Option(subcat[i][0], subcat[i][2]);
}
}
//保存选择参数=========貌似这块没什么用!!!
for (i=0;i<document.newform.city.length;i++){
//alert(document.myform.smallclass[i].value);
if (document.newform.city[i].value==document.newform.cityselect.value){
document.newform.city[i].selected=true;
}
}
}
</script>
<form name="myform" method="post">
<select name="bigclass" onChange="changelocation(document.myform.bigclass.options[document.myform.bigclass.selectedIndex].value)">
<%
sql1 = "SELECT * FROM ProvinceCityList where parent=0"
set rs1 = conn.Execute (sql1)
do while not rs1.eof
%>
<option value="<%=rs1("id")%>"><%=rs1("city")%></option>
<%
rs1.movenext
loop
rs1.close
set rs1 = nothing
conn.Close
set conn = nothing
%>
</select>
<select name="smallclass">
<option selected value="">==所有城市==</option>
</select>
</form>
<script LANGUAGE="javascript">
changelocation(document.myform.bigclass.options[document.myform.bigclass.selectedIndex].value);
</script>