全国省市县区街道 无联动下拉选择 数据库及源代码
前端JQ代码
引入jq库
<script language="javascript">
$(document).ready(function() {
// 加载所有的省份
$.ajax({
type: "get",
url: "{:U('Lease/region')}", // type=1表示查询省份
dataType: "json",
success: function(data) {
$("#provinces").html("<option value=''>请选择省份</option>");
$.each(data, function(i, item) {
$("#provinces").append("<option value='" + item.provice_id + "'>" + item.provice_name + "</option>");
});
}
});
$("#provinces").change(function() {
$.ajax({
type: "get",
url: "{:U('Lease/regioncity')}", // type =2表示查询市
data: {
"province_id": $(this).val(),
},
dataType: "json",
success: function(data) {
$("#citys").css('display','block');
$("#citys").html("<option value=''>请选择市</option>");
$.each(data, function(i, item) {
$("#citys").append("<option value='" + item.city_id + "'>" + item.city_name + "</option>");
});
}
});
});
$("#citys").change(function() {
$.ajax({
type: "get",
url: "{:U('Lease/regioncounty')}", // type =2表示查询市
data: {
"city_id": $(this).val(),
},
dataType: "json",
success: function(data) {
$("#countys").css('display','block');
$("#countys").html("<option value=''>请选择县</option>");
$.each(data, function(i, item) {
$("#countys").append("<option value='" + item.county_id + "'>" + item.county_name + "</option>");
});
}
});
});
$("#countys").change(function() {
$.ajax({
type: "get",
url: "{:U('Lease/regiontown')}", //
data: {
"county_id": $(this).val(),
},
dataType: "json",
success: function(data) {
$("#towns").css('display','block');
$("#towns").html("<option value=''>请选择街道</option>");
$.each(data, function(i, item) {
$("#towns").append("<option value='" + item.town_id + "'>" + item.town_name + "</option>");
});
}
});
});
$("#towns").change(function() {
$.ajax({
type: "get",
url: "{:U('Lease/regionvillage')}", //
data: {
"town_id": $(this).val(),
},
dataType: "json",
success: function(data) {
$("#villages").css('display','block');
$("#villages").html("<option value=''>请选择社区</option>");
$.each(data, function(i, item) {
$("#villages").append("<option value='" + item.village_id + "'>" + item.village_name + "</option>");
});
}
});
});
});
</script>
html代码
<div>
<select name="address[]" id="provinces" style="display: block;float: left;">
<option value="">请选择省份</option>
</select>
<select name="address[]" id="citys" style="display: none;float: left;">
<option value="">请选择市</option>
</select>
<select name="address[]" id="countys" style="display: none;float: left;">
<option value="">请选择县</option>
</select>
<select name="address[]" id="towns" style="display: none;float: left;">
<option value="">请选择街道</option>
</select>
<select name="address[]" id="villages" style="display: none;float: left;">
<option value="">请选择社区</option>
</select>
</div>
thinkphp代码
//读取省数据库
public function region(){
$provinces=M('Position_provice')->select();
$provinces_json = json_encode($provinces);
exit($provinces_json);
}
//读取市数据库
public function regioncity(){
$where['province_id']=$_GET['province_id'];
$provinces=M('Position_city')->where($where)->select();
$provinces_city = json_encode($provinces);
exit($provinces_city);
}
//读取省/区数据库
public function regioncounty(){
$where['city_id']=$_GET['city_id'];
$provinces=M('Position_county')->where($where)->select();
$provinces_county = json_encode($provinces);
exit($provinces_county);
}
//读取街道数据库
public function regiontown(){
$where['county_id']=$_GET['county_id'];
$provinces=M('Position_town')->where($where)->select();
$provinces_town = json_encode($provinces);
exit($provinces_town);
}
//读取社区数据库
public function regionvillage(){
$where['town_id']=$_GET['town_id'];
$provinces=M('Position_village')->where($where)->select();
$provinces_village = json_encode($provinces);
exit($provinces_village);
}