jsp:
<xx:selectArea id="province" name="custAreaProvince" style="width:135px" onChange="selectProvince(this.value)"/>
<xx:selectArea id="city" name="custArea" style="width:140px"/>
这是自定义的标签,可根据需要自行定义或者适应标准的input标签,如<select
style="text-indent: 0px;" class="select_style1" id="systemName"
name="systemName" onChange="getData()">
<c:forEach items="${list}" var="req" varStatus="status">
<option value="${req.systemName}">${req.systemName}</option>
</c:forEach>
</select></li>
JS:
function selectProvince(province){
$.ajax({
cache: true,
type: "post",
scriptCharset: "UTF-8",
url:"${ctx}/common/getCityByProvince.htm",
data:{province:province},
async: false,
success: function(data) {
if(null == data || "" == data){
return;
}
//清空select
$("#city").empty();
var options = data;
//给select赋值
$.each(options, function(i) {
$('#city').append('<option value="' + options[i].areaName + '">' + options[i].areaName + '</option>');
});
}
});
}
Java:
@RequestMapping(value = "getCityByProvince", method = RequestMethod.POST)
@ResponseBody
public List<TArea> getCityByProvince(String province) {
List<Map<String, List<TArea>>> list = null;
List<TArea> areas = null;
try {
list = DataServerSingleton.getInstance().getArea();
} catch (Exception e) {
e.printStackTrace();
return areas;
}
Boolean flag = Boolean.TRUE;
for (Map<String, List<TArea>> map : list) {
Set keySet = map.keySet();
Iterator<String> provinceIt = keySet.iterator();
if (!flag) {
break;
}
while (provinceIt.hasNext() && flag) {
String key = provinceIt.next();
if (key.equalsIgnoreCase(province)) {
areas = map.get(key);
flag = Boolean.FALSE;
}
}
}
return areas;
}