ci框架 省市三级联动
只要在ci中引入这个就可以了简单
<?php $data['province_selected'] = $address['province_id']; $data['city_selected'] = $address['city_id']; $data['district_selected'] = $address['district_id']; $this->load->view('district_select',$data); ?>
1. [文件] district_select.php
<?php $CI = get_instance(); $CI->load->model('region_model', 'region'); $provinces = $CI->region->provinces(); $citys = $CI->region->children_of($province_selected); ?> <script language="JavaScript"> <?php if(isset($province_selected)):?> var province_selected = <?php echo (int)$province_selected?>; <?php else:?> var province_selected = 0; <?php endif?> <?php if(isset($city_selected)):?> var city_selected = <?php echo (int)$city_selected?>; <?php else:?> var city_selected = 0; <?php endif?> <?php if(isset($district_selected)):?> var district_selected = <?php echo (int)$district_selected?>; <?php else:?> var district_selected = 0; <?php endif?> $(document).ready(function() { var change_city = function(){ $.ajax({ url: '<?php echo ITURL.'/admin.php/region_change/select_children/parent_id' // site_url('region_change/select_children/parent_id')?>'+'/'+$('#province_id').val(), type: 'GET', dataType: 'html', success: function(data){ city_json = eval('('+data+')'); var city = document.getElementById('city_id'); city.options.length = 0; city.options[0] = new Option('城市', '-11'); for(var i=0; i<city_json.length; i++){ var len = city.length; city.options[len] = new Option(city_json[i].region_name, city_json[i].region_id); if (city.options[len].value == city_selected){ city.options[len].selected = true; } } change_district();//重置地区 } }); } change_city();//初始化城市 $('#province_id').change(function(){ change_city(); }); var change_district = function(){ $.ajax({ url: '<?php echo ITURL.'/admin.php/region_change/select_children/parent_id' //site_url('region_change/select_children/parent_id')?>'+'/'+$('#city_id').val(), type: 'GET', dataType: 'html', success: function(data){ district_json = eval('('+data+')'); var district = document.getElementById('district_id'); district.options.length = 0; district.options[0] = new Option('县/区', '-22'); for(var i=0; i<district_json.length; i++){ var len = district.length; district.options[len] = new Option(district_json[i].region_name, district_json[i].region_id); if (district.options[len].value == district_selected){ district.options[len].selected = true; } } } }); } $('#city_id').change(function(){ change_district(); }); }); </script> <select name="province_id" id="province_id" style="width:100px;"> <option value="-1" >省份</option> <?php foreach($provinces as $key => $province): ?> <option value="<?php echo $province['region_id']; ?>" <?php if($province['region_id']==$province_selected){echo 'selected';}?> > <?php echo $province['region_name']; ?> </option> <?php endforeach; ?> </select> <select name="city_id" id="city_id" style="width:100px;"> </select> <select name="district_id" id="district_id" style="width:100px;"> <option value=""></option> </select>
2. [文件] region_model.php
<?php /** * 省市县 * * */ class Region_Model extends CI_Model { /** * * * @return Region_Model */ function Region_Model() { parent::__construct(); } // -------------------------------------------------------------------- /** * * * @param integer $parent_id */ function children_of($parent_id, $select="*") { $parent_id = (int)$parent_id; $regions = array(); $this->db->select($select); $this->db->where('parent_id', $parent_id); if ($query = $this->db->get('region')){ return $query->result_array(); } return array(); } // -------------------------------------------------------------------- /** * * * @return array */ function provinces() { return $this->children_of(1); } // -------------------------------------------------------------------- /** * 区域名 * * */ function get_name($id) { if (!$id){ return array(); } $this->db->select('region_name'); $query = $this->db->get_where('region',array('region_id' => $id)); if ($row = $query->row_array()){ return $row['region_name']; } return array(); } // -------------------------------------------------------------------- /** * load by id * * */ function load($id) { if (!$id){ return array(); } $query = $this->db->get_where('region',array('region_id' => $id)); if ($row = $query->row_array()){ return $row; } return array(); } }
以上就是ci框架 省市三级联动的内容,更多相关内容请关注PHP中文网(www.php.cn)!
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
刺客信條陰影:貝殼謎語解決方案
3 週前ByDDD
Windows 11 KB5054979中的新功能以及如何解決更新問題
2 週前ByDDD
在哪裡可以找到原子中的起重機控制鑰匙卡
3 週前ByDDD
<🎜>:死鐵路 - 如何完成所有挑戰
4 週前ByDDD
Atomfall指南:項目位置,任務指南和技巧
1 個月前ByDDD

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!