87,904
社区成员
发帖
与我相关
我的任务
分享
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url : "${request.contextPath}/xml/address.xml",
success : function(xml) {
$(xml).find("province").each(function(){
var t = $(this).attr("name");
$("#DropProvince").append(
"<option>" + t + "</option>");
});
}
});
//二级的菜单
$("#DropProvince").change(
function() {
$("#sCity>option").remove();
var pname = $("#DropProvince").val();
$.ajax({
url : "${request.contextPath}/xml/address.xml",
success : function(xml){
$(xml).find("province[name='" + pname+ "']>city").each(function(){
var c = $(this).attr("name")
$("#sCity").append("<option>"+ c + "</option>");
});
}
});
}
);
//三级的菜单
$("#sCity").change(
function() {
$("#sAddress>option").remove();
var pname1 = $("#sCity").val();
$.ajax({
url : "${request.contextPath}/xml/address.xml",
success : function(xml) {
$(xml).find("city[name='" + pname1+ "']>address").each(function(){
var d = $(this).attr("name")
$("#sAddress").append("<option>"+ d + "</option>");
});
}
});
}
);
});
</script>
<tr>
<td align="center">省: <select id="DropProvince" name="province"><option>请选择</option></select></td>
</tr>
<tr>
<td align="center">市: <select id="sCity" name="city"><option>请选择</option></select></td>
</tr>
<tr>
<td align="center">区/县: <select id="sAddress" name="address"><option>请选择</option></select></td>
</tr>
var pro='黑龙江',city='哈尔滨',area='南岗区'//将读出的值赋值给这3个变量,添加option的时候对比下相同添加selected属性
$(document).ready(function(){
$.ajax({
url : "${request.contextPath}/xml/address.xml",
success : function(xml) {
$(xml).find("province").each(function(){
var t = $(this).attr("name");
$("#DropProvince").append(
"<option"+(t==pro?' selected':'')+">" + t + "</option>");///////////
});
}
});
//二级的菜单
$("#DropProvince").change(
function() {
$("#sCity>option").remove();
var pname = $("#DropProvince").val();
$.ajax({
url : "${request.contextPath}/xml/address.xml",
success : function(xml){
$(xml).find("province[name='" + pname+ "']>city").each(function(){
var c = $(this).attr("name")
$("#sCity").append("<option"+(c==city?' selected':'')+">"+ c + "</option>");/////////
});
}
});
}
);
//三级的菜单
$("#sCity").change(
function() {
$("#sAddress>option").remove();
var pname1 = $("#sCity").val();
$.ajax({
url : "${request.contextPath}/xml/address.xml",
success : function(xml) {
$(xml).find("city[name='" + pname1+ "']>address").each(function(){
var d = $(this).attr("name")
$("#sAddress").append("<option"+(d==area?' selected':'')+">"+ d + "</option>");///////////
});
}
});
}
);
});