jquery+xml 二级联动

XML格式如下:

<?xml version="1.0" encoding="utf-8" ?>
<channel>
<area id="1">
<city name="珠海" />
<city name="深圳" />
<city name="中山" />
</area>
<area id="2">
<city name="成都" />
<city name="广安" />
</area>
<area id="3">
<city name="武汉" />
<city name="宜昌" />
</area>
<area id="4">
<city name="长沙" />
</area>
</channel>

  

前台html:

<select id="root">
<option value="1">广东</option>
<option value="2">四川</option>
<option value="3">湖北</option>
<option value="4">湖南</option>
</select>
<select id="son" name="son">
</select>

  

JavaScript:

<script type="text/javascript" src="scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
function getson(fpath, vals) {
$.ajax({
type:
'GET',
url: fpath,
dataType:
'xml',
beforeSend:
function () {
$(
'#son').empty();
},
success:
function (data) {
$(data).find(
"area[id='" + vals + "']>city").each(
function () {
var son = $(this).attr("name");
$(
"#son").append('<option value="' + son + '">' + son + '</option>');
}
);
}
});
}

$(
function () {
$(
'#root').change(function () {
getson(
'xml/area.xml', $(this).val());
}).trigger(
'change');
});
</script>

效果预览:  

  

转载于:https://www.cnblogs.com/elycir/archive/2011/08/21/2147977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值