jquery实现的省市区三级联动
省市级联动,附使用示例和数据表数据
有部分数据精确到乡镇一级!!!
Git地址:http://git.oschina.net/upliu/province-city-district
演示代码:
<!DOCTYPEhtml>
<html>
<headlang="en">
<metacharset="UTF-8">
<title>省市区三级联动</title>
</head>
<body>
<formmethod="post"action="post.php">
<divid="area"></div>
<inputtype="submit"style="margin-top:10px;">
</form>
<scriptsrc="jquery-2.1.3.min.js"></script>
<scriptsrc="area.js"></script>
<script>
$(function(){
add_select(0);
$('body').on('change','#areaselect',function(){
var$me=$(this);
var$next=$me.next();
/**
*如果下一级已经是当前所选地区的子地区,则不进行处理
*/
if($me.val()==$next.data('pid')){
return;
}
$me.nextAll().remove();
add_select($me.val());
});
functionadd_select(pid){
vararea_names=area['name'+pid];
if(!area_names){
returnfalse;
}
vararea_codes=area['code'+pid];
var$select=$('<select>');
$select.attr('name','area[]');
$select.data('pid',pid);
if(area_codes[0]!=-1){
area_names.unshift('请选择');
area_codes.unshift(-1);
}
for(varidxinarea_codes){
var$option=$('<option>');
$option.attr('value',area_codes[idx]);
$option.text(area_names[idx]);
$select.append($option);
}
$('#area').append($select);
};
});
</script>
</body>
</html>
以上所述就是本文给大家分享的全部内容了,希望大家能够喜欢。