实现两个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里面