實(shí)現(xiàn)兩個(gè)Web下拉選擇框關(guān)聯(lián)
比如要實(shí)現(xiàn)國(guó)家Country與城市City的關(guān)聯(lián):
動(dòng)態(tài)javascript方法實(shí)現(xiàn)如下:
<!-- 引入js文件-->
<script src="/scripts/xselect.js"></script>
<script language="javascript">
<!--
var dsy = new Dsy();
//countryID是父select控件名稱, cityID是子select控件名稱.
var sel = ["countryID","cityID"];
//定義父與子兩個(gè)select控件的默認(rèn)值
var def = ['<c:out value="${storeForm.countryID}" />','<c:out value="${storeForm.cityID}" />'];
//父select控件內(nèi)容的定義
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控件的內(nèi)容
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類型轉(zhuǎn)換為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>
//注意,上述代碼寫(xiě)到html form里面