您的位置:首页 > Web前端 > JQuery

Jquery实现ajax三级联动

2016-03-17 17:36 651 查看
jquery下ajax的使用:

$.ajax(
{
url:'';
data:'',
dataType:'',
type:'',
success:func,
async:true,
cache:true
}
);


实现三级联动例子



<script src="jquery-1.12.1.min.js"></script>
<script>
$(function(){
$.ajax(
{
url:'./Provinces.xml',
type:'get',
dataType:'xml',
success:function(msg){
$(msg).find('Province').each(function(k,v){
var name = $(v).attr('ProvinceName');
var id = $(v).attr('ID');
$('select:eq(0)').append('<option value='+id+'>'+name+'</option>');
});
}
}
);
//将第三个select清空
$('select:eq(0)').bind('change',function(){
$('select:eq(2)').empty();
$('select:eq(2)').append('<option value=0>select</option>');
});
$('select:eq(0)').bind('change',function(){
//获得第一个select的ID
var num = $('select:eq(0) option:selected').attr('value');
//每次都要进行清空
$('select:eq(1)').empty();
$('select:eq(1)').append('<option value=0>select</option>');
$.ajax(
{
url:'./Cities.xml',
type:'get',
dataType:'xml',
success:function(msg){
$(msg).find('City[PID='+num+']').each(function(k,v){
var name = $(v).attr('CityName');
var id = $(v).attr('ID');
$('select:eq(1)').append('<option value='+id+'>'+name+'</option>');
});
}
}
);

});

$('select:eq(1)').bind('change',function(){
var num = $('select:eq(1) option:selected').attr('value');
//                console.log(num);
$('select:eq(2)').empty();
$.ajax(
{
url:'./Districts.xml',
type:'get',
dataType:'xml',
success:function(msg){
$(msg).find('District[CID='+num+']').each(function(k,v){
var name = $(v).attr('DistrictName');
var id = $(v).attr('ID');
$('select:eq(2)').append('<option value='+id+'>'+name+'</option>');
});
}
}
);

});

});
</script>


源码与相关文件(jquery与xml)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajax