标签:省市区三级联动
数据库文件:http://pan.baidu.com/s/1dDEkTLB
1,
<script language="javascript" type="text/javascript" src="jquery.js"></script>
2,
<script>function setChild(parent_node, parent_id) { if(‘city_id‘ == parent_node) { $(‘#city_id‘).html(‘<option value="">选择市</option>‘); $(‘#zone_id‘).html(‘<option value="">选择区/县</option>‘); } if(‘zone_id‘ == parent_node) { $(‘#zone_id‘).html(‘<option value="">选择区/县</option>‘); } var url = ‘<?php echo U(‘Mypanel/ajax_setChild‘); ?>&parent_id=‘ + parent_id + ‘&rand=‘ + Math.random(); $.get(url, {}, function(data) { $(‘#‘ + parent_node).append(data); }); } </script>
3,
<select name="province_id" id="province_id" class="select" onChange="setChild(‘city_id‘, this.value)" style="width:100px;"> <option value="">选择省</option> <?php foreach($provinceArr as $item) { ?> <option value="<?php echo $item[‘code‘]; ?>"><?php echo $item[‘name‘];?></option> <?php } ?> </select> <select name="city_id" id="city_id" class="select" onChange="setChild(‘zone_id‘, this.value)" style="width:100px;"> </select> <select name="zone_id" id="zone_id" class="select" style="width:100px;"> </select>
4,MypanelController.class.php
<?php /** * 公用 */ namespace Admin\Controller; use Common\Controller\AdminbaseController; class MypanelController extends AdminbaseController{ private $_address_model = null; function _initialize() { parent::_initialize(); $this->_address_model = D(‘Address‘); } public function ajax_setChild() { $parent_id = intval($_GET[‘parent_id‘]); $data = $this->_address_model->getData($parent_id); $optionStr = ‘‘; foreach($data as $v) { $optionStr .= ‘<option value="‘.$v[‘code‘].‘">‘.$v[‘name‘].‘</option>‘; } echo $optionStr; } }
5,AddressModel.class.php
<?php /* * * 后台权限 */ namespace Common\Model; use Common\Model\CommonModel; class AddressModel extends CommonModel { public function getData($parentId=0) { $data = $this->where(array(‘parentId‘=>$parentId))->order(‘id ASC‘)->select(); return $data; } } ?>
标签:省市区三级联动
原文地址:http://richang.blog.51cto.com/9879853/1622272