thinkphp中实现省市区三级联动菜单
数据库文件: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; } } ?>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。