多级联动实现思路及代码

  1. 一下是视图层代码  
  2. <!DOCTYPE html>  
  3. <html>  
  4. <head lang="en">  
  5.     <meta charset="UTF-8">  
  6.     <title></title>  
  7. </head>  
  8. <body>  
  9. <table>  
  10. <select  id="countrys" οnchange="sheng('countrys','sheng')"//括号里第一形参 需要与id一致 方便下面接受id  
  11.     <option value="-1">请选择</option>  
  12.     <?php foreach($a as $v){  
  13.     echo "<option value='".$v['region_id']."'>".$v['region_name']."</option>"//单引号解析$ 双引号不解析 所以需要拼结  
  14.     }?>  
  15. </select>  
  16.     <select name="" id="sheng" οnchange="sheng('sheng','shi')"// id需要与上面括号第二形参一致 用户接受数据  
  17.         <option value="-1">请选择</option>  
  18.     </select>  
  19.     <select name="" id="shi" οnchange="sheng('shi','xian')">  
  20.         <option value="-1">请选择</option>  
  21.     </select>  
  22.     <select name="" id="xian">  
  23.         <option value="0">请选择</option>  
  24.     </select>  
  25.   
  26. </table>  
  27. </body>  
  28. </html>  
  29. <script src="<?php echo base_url('jquery-2.1.4.min.js') ?>"></script>  
  30. <script>  
  31.     function sheng(cursel,nextsel){  
  32.         var id=$("#"+cursel).val();  
  33.         //alert(id);  
  34.         $.ajax({  
  35.             type:"POST",  
  36.             url:"<?php echo site_url('Welcome/country') ?>",  
  37.             data:"id="+id,  
  38.             dataType:"json",  
  39.             success: function(msg)  
  40.             {  
  41.   
  42.                var str="<option value='-1'>请选择</option>";  
  43.                for(i in msg)  
  44.                {  
  45.                    str+="<option value='"+msg[i]['region_id']+"'>"+msg[i]['region_name']+"</option>";  
  46.                }  
  47.              $("#"+nextsel).html(str);  
  48.            }  
  49.         });  
  50.     }  
  51. </script>  
  52.   
  53. 控制器代码  
  54. <pre name="code" class="php"><?php  
  55. defined('BASEPATH') OR exit('No direct script access allowed');  
  56.   
  57. class Welcome extends CI_Controller {  
  58.    public function index()  
  59.    {  
  60.         $data['a']=$this->db->get_where('ecs_region',array('parent_id'=>0))->result_array(); //数据库用的表 是ecshop的地区表 查询0的地区  
  61.   
  62.        // var_dump($data);  
  63.       $this->load->view('lian.html',$data);  
  64.    }  
  65.     public  function  country(){  
  66.         $id=$_POST['id'];  
  67.         $sheng=$this->db->get_where('ecs_region',array('parent_id'=>$id))->result_array();//查询对应下id的地区  
  68.          echo json_encode($sheng); //转为json格式  
  69.   
  70.     }  
  71. }  
  72.   
  73.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值