下面是个2级联动的实例:
<html>
<head>
<title>新增作业场所职业危害信息</title>
<link rel="stylesheet" type="text/css" href="../css/css_new.css">
</head>
<body>
<form name="addform" action="#" method="post" onSubmit="return beforesubmit()">
<table>
<tr>
<td><font size="-1">名称:</font></td>
<td><font size="-1">
<select id=province name="province"></select>
<select id=city name="city"></select>
<input type=hidden name="tprovince" size=10 readonly>
<input type=hidden name="tcity" size=10 readonly>
</font>
</td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">
function LianDong(arr,sel,t)
{
var me = this;
this.$ = function(o)
{
return document.getElementById(o);
}
this.sub = function (i,pid)
{
if (i>=0)
{
me.$(t[i]).value=me.$(sel[i]).value;
}
for (var j=i+1; j<sel.length; j++)
{
me.$(sel[j]).length = 0;
me.$(sel[j]).options[0] = new Option("请选择", "");
}
for ( var j = 0; j < arr.length; j++)
{
if (arr[j][1] == pid)
{
me.$(sel[i+1]).options[me.$(sel[i+1]).length] = new Option(arr[j][2], arr[j][0]);
}
}
}
this.init = function()
{
me.sub(-1,"root");
for (var i=0; i<sel.length; i++)
{
me.$(sel[i]).onchange = function()
{
var i;
for (i=0; me.$(sel[i])!=this; i++);
me.sub(i, me.$(sel[i]).value);
}
}
}
this.SetValue = function()
{
}
this.init();
}
var array=new Array();
array[0]=new Array("江苏省","root","江苏省");
array[1]=new Array("辽宁省","root","辽宁省");
array[2]=new Array("南京市","江苏省","南京市");
array[3]=new Array("无锡市","江苏省","无锡市");
array[4]=new Array("大连市","辽宁省","大连市");
array[5]=new Array("沈阳市","辽宁省","沈阳市");
var select = new Array("province","city");
var t = new Array("tprovince","tcity");
var liandong=new LianDong(array, select, t)
liandong.SetValue('请选择','请选择');
</script>
问题是:
如果是第一次选择,选择框默认的值分别是“请选择”和“请选择”;如果当前页面已经从上个页面中获取了数值,如province='辽宁省',city='大连市',如果让选择框中默认的数值分别是'辽宁省'和大连市',二级联动功能保留?
------解决方案--------------------
<script type="text/javascript">
var city=[
["北京","天津","上海","重庆","香港","澳门"],
["石家庄","唐山","秦皇岛","邯郸","邢台","保定","张家口","承德","沧州","廊坊","衡水"],
["太原","大同","阳泉","长治","晋城","朔州","晋中","运城","忻州","临汾","吕梁"],