實現兩個Web下拉選擇框關聯
比如要實現國家Country與城市City的關聯:
動態javascript方法實現如下:
<!-- 引入js文件-->
<script src="/scripts/xselect.js"></script>
<script language="javascript">
<!--
var dsy = new Dsy();
//countryID是父select控件名稱, cityID是子select控件名稱.
var sel = ["countryID","cityID"];
//定義父與子兩個select控件的默認值
var def = ['<c:out value="${storeForm.countryID}" />','<c:out value="${storeForm.cityID}" />'];
//父select控件內容的定義
dsy.add("0",[['-------','0']
<logic:iterate name="CountryList" id="country">
,['<bean:write name="country" property="label"/>','<bean:write name="country" property="value"/>']
</logic:iterate>
]);
//下面定義子select控件的內容
dsy.add("0_0",[['------','0']]);
<logic:iterate name="CountryList" id="country" indexId="index">
dsy.add('0_<%=index.intValue()+1%>',[['------','0']
<logic:iterate name="Citys" id="city">
<bean:define id="c" name="city" property="countryID" />
<bean:define id="c2" name="country" property="value" />
//由于city.countryID是long類型,而country.value是String類型,所以把long類型轉換為String類型
<%
String str = c + "";
%>
//遍歷所有的City List, 如果city的countryID與country id一致,就加入select控件
<logic:equal name="country" property="value" value="<%=str%>">
,['<bean:write name="city" property="name"/>','<c:out value="${city.id}" />']
</logic:equal>
</logic:iterate>]);
</logic:iterate>
attachSelect(dsy,sel,def);
// -->
</script>
//注意,上述代碼寫到html form里面