1、js代码
<script type="text/javascript" src="<%=basePath%>js/jquery-1.7.2.js"></script>
<script type="text/javascript">
// 获取省市地区
$(document).ready(function(){
$.post("<%=basePath%>searchCityAction!searchCity",function(dataObj){
document.getElementById("city").options.length=0; // 将原有的选项清空
var optgroupTop = document.createElement("OPTGROUP"); // 创建一个 optgroup
optgroupTop.label="";
document.getElementById("city").appendChild(optgroupTop);// 将新建的optgroup添加到select中
for(var j = 0; j < dataObj.length; j++){ // 遍历城市
if(j == 0){
var optgroup = document.createElement("OPTGROUP"); // 创建一个optgroup
optgroup.label = dataObj[j][0];
document.getElementById("city").appendChild(optgroup); // 将新建的optgroup添加到select中
}
if(j != 0 && dataObj[j][0] != dataObj[j-1][0]){
var optgroup = document.createElement("OPTGROUP"); // 创建一个optgroup
optgroup.label = dataObj[j][0];
document.getElementById("city").appendChild(optgroup); // 将新建的optgroup添加到select中
}
var option = document.createElement("option"); // 创建一个option
option.text = dataObj[j][2]; // 设置显示的值
option.value = dataObj[j][1]; // 设置value属性值
document.getElementById("city").options.add(option); // 将新建的option添加到select中
}
},"json");
});
</script>
2、html代码
<td style="FONT-SIZE: 14px" height=40 align=right>所在城市:</td >
<td >
<select id=city name=city>
</select>
</td >