养殖场-》栋舍级联菜单
//获取默认养殖场和栋舍信息
1 public function sbjr(){ 2 3 $yzc_model=M("Yzc"); 4 5 $list = $yzc_model 6 ->field("id, mingcheng") 7 ->where("shenhe=1") 8 ->order("id ASC") 9 ->limit($page->firstRow . ',' . $page->listRows) 10 ->select(); 11 12 $juanshe_model=M("Juanshe"); 13 14 $list1 = $juanshe_model 15 ->field("id, mingcheng") 16 ->where("yzcid=".$list[0]['id']) 17 ->order("id ASC") 18 ->limit($page->firstRow . ',' . $page->listRows) 19 ->select(); 20 21 $this->assign('list1', $list1); 22 $this->assign('list', $list); 23 $this->display(); 24 }
//前端模板显示
1 <table class="table table-bordered"> 2 <tr> 3 <th>养殖场名称</th> 4 <td> 5 <select name="yzcmc" id="yzcmc"> 6 <foreach name="list" item="vo"> 7 <option value="{$vo.id}">{$vo.mingcheng}</option> 8 </foreach> 9 </select> 10 <span class="form-required">*</span> 11 </td> 12 </tr> 13 <tr> 14 <th>栋舍名称</th> 15 <td> 16 <select name="dsmc" id="dsmc"> 17 <foreach name="list1" item="vo"> 18 <option value="{$vo.id}">{$vo.mingcheng}</option> 19 </foreach> 20 </select> 21 <span class="form-required">*</span> 22 </td> 23 </tr> 24 </table>
前端js操作,ajax获取栋舍信息
<script type="text/javascript"> $('#yzcmc').change(function(){ var objectModel = {}; var value = $(this).val(); objectModel['id']=value; $.ajax({ cache:false, type:"POST", url:"{:U('Setting/getjuanshe')}", dataType:"json", data:objectModel, timeout:30000, error:function(){ alert("{:U('Setting/getjuanshe')}"); }, success:function(data){ $("#dsmc").empty(); var count = data.length; console.log(count); console.log(data); var i = 0; var b=""; for(i=0;i<count;i++){ b+="<option value='"+data[i].id+"'>"+data[i].mingcheng+"</option>"; } $("#dsmc").append(b); } }); }); </script>
thinkphp后端返回栋舍
1 //获取圈舍 级联菜单 2 public function getjuanshe(){ 3 4 $id=I('request.id'); 5 $juanshe_model=M("Juanshe"); 6 7 $list = $juanshe_model 8 ->field("id, mingcheng") 9 ->where("yzcid=".$id) 10 ->order("id ASC") 11 ->select(); 12 13 //$this->ajaxReturn($list,"JSON"); 14 echo json_encode($list); 15 }