省市二级联动,只需要传入provsCitysMap的map集合,省市的id值即可。
<script type="text/javascript">
var provs = [
<c:forEach items="${provsCitysMap}" var="ps" varStatus="pi">
{ ctId:"${ps.key.cityId}",
ctName:"${ps.key.cityName }",
cities:[
<c:forEach var="cts" items="${ps.value }">
{
ctId:"${cts.cityId }",
ctName:"${cts.cityName }"
},
</c:forEach>
{}
],
},
</c:forEach>{}
];
var prov = document.getElementById('prov');
//添加省
for(var i = 0; i < provs.length-1; i++){
var op = new Option();
op.value = provs[i].ctId;
op.text = provs[i].ctName;
prov.options.add(op);
}
function toChangeCity(){
//取选中省的value
var provValue = document.getElementById("prov").value;
//获取市列表的对象
var city = document.getElementById("city");
//清空市内容
for(var i = 0; i < city.options.length; ){
city.options.remove(0);
}
//以value取市的对象//添加到列表中
var selectProv = null;
for(var i = 0; i < provs.length; i++){
if(provValue == provs[i].ctId){
selectProv = provs[i];
}
}
for(var i = 0; i < selectProv.cities.length-1; i++){
var op = new Option();
op.value = selectProv.cities[i].ctId;
op.text = selectProv.cities[i].ctName;
city.options.add(op);
}
}
</script>