// 初始化地址列表
addressInit();
function addressInit() {
$.get('order.php?act=address', function (resp) {
// 测试是否存在收货地址
if (0 == resp.data.length) {
return false;
}
//展示地址
$('#al_01').empty();
$.each(resp.data, function (i, address) {
var option = '<li>';
option += '<span>'+address.consignee+'</span>';
option += '<span id='+address.address_id+'>'+address.r1_title+' '+address.r2_title+' '+address.r3_title+' '+address.address+' '+address.mobile;
option += '</span>';
option += '<a href="javascript:del_add('+address.address_id+')">删除</a>';
option += '<a href="javascript:edit_add('+address.address_id+')" class="click_pop">修改</a>';
option += '</li>';
$('#al_01').append(option);
});
}, 'json');
}
function setRegion(id, level) {
// 下个级别
var next = +level + 1;
// 设置最大地区
var maxLevel = 3;
// ajax获取子地区
$.get('order.php?act=child', {region_id: id}, function (resp) {
$('#input-address-region_id_' + next + '>option:gt(0)').remove();
$.each(resp.region, function (i, region) { // option += address.is_last == '1' ? ' selected' : '';
var option = '<option value="' + region.region_id + '">';
option += region.region_name;
option += '</option>';
// 加入select(next)
$('#input-address-region_id_' + next).append(option);
});
}, 'json');
//清空 后续的select
for (var i = next + 1; i <= maxLevel; ++i) {
$('#input-address-region_id_' + i + '>option:gt(0)').remove();
}
}
$(function () {
// 初始化1级地区
setRegion(1, 0);
// 绑定select的change事件
$('select[id^=input-address-region_id_]').change(function () {
// 请选择, 不触发
if ('' == $(this).val()) {
return;
}
setRegion($(this).val(), $(this).data('level'));
});
})
<li>
<label class="left"><b>*</b> 所在地区:</label>
<div class="formlisttext" id="add">
<select class="small-input" name="region_id_1" id="input-address-region_id_1" data-level="1">
<option value=""> 请选择 </option>
</select>
<select class="small-input" name="region_id_2" id="input-address-region_id_2" data-level="2">
<option value=""> 请选择 </option>
</select>
<select class="small-input" name="region_id_3" id="input-address-region_id_3" data-level="3">
<option value=""> 请选择 </option>
</select>
</div>
</li>
function action_child(){
# 获取会员id
$db = $GLOBALS['db'];
$ecs = $GLOBALS['ecs'];
$user_id = $_SESSION['user_id'];
if(isset($_GET['address_id'])){
# 地址列表
$add_sql = "select a.*, r1.region_name as r1_title, r2.region_name as r2_title, r3.region_name as r3_title from"
.$ecs->table('user_address')." as a left join "
.$ecs->table('region')." as r1 on a.province=r1.region_id left join "
.$ecs->table('region')." as r2 on a.city=r2.region_id left join "
.$ecs->table('region')." as r3 on a.district=r3.region_id "
." where a.user_id=".$user_id." AND a.address_id=".$_GET['address_id'];
$address_list = $db->getRow($add_sql);
}
if(isset($_GET['region_id'])){
$id = $_GET['region_id'];
$sql = "select * from".$ecs->table('region').' where parent_id='.$id;
$rows = $db->getAll($sql);
}else{
$sql = "select * from".$ecs->table('region');
$rows = $db->getAll($sql);
}
$arr = array('add'=>$address_list,'region'=>$rows);
echo json_encode($arr);
die;
}
function action_address()
{
# 获取会员id
$db = $GLOBALS['db'];
$ecs = $GLOBALS['ecs'];
$user_id = $_SESSION['user_id'];
# 地址列表
$add_sql = "select a.*, r1.region_name as r1_title, r2.region_name as r2_title, r3.region_name as r3_title from"
.$ecs->table('user_address')." as a left join "
.$ecs->table('region')." as r1 on a.province=r1.region_id left join "
.$ecs->table('region')." as r2 on a.city=r2.region_id left join "
.$ecs->table('region')." as r3 on a.district=r3.region_id "
." where a.user_id=".$user_id." and status=0";
$address_list = $db->getAll($add_sql);
echo json_encode(array('data' => $address_list));
}