养殖场-》栋舍级联菜单

//获取默认养殖场和栋舍信息

 public function sbjr(){

         $yzc_model=M("Yzc");

         $list = $yzc_model
->field("id, mingcheng")
->where("shenhe=1")
->order("id ASC")
->limit($page->firstRow . ',' . $page->listRows)
->select(); $juanshe_model=M("Juanshe"); $list1 = $juanshe_model
->field("id, mingcheng")
->where("yzcid=".$list[0]['id'])
->order("id ASC")
->limit($page->firstRow . ',' . $page->listRows)
->select(); $this->assign('list1', $list1);
$this->assign('list', $list);
$this->display();
}

//前端模板显示

 <table class="table table-bordered">
<tr>
<th>养殖场名称</th>
<td>
<select name="yzcmc" id="yzcmc">
<foreach name="list" item="vo">
<option value="{$vo.id}">{$vo.mingcheng}</option>
</foreach>
</select>
<span class="form-required">*</span>
</td>
</tr>
<tr>
<th>栋舍名称</th>
<td>
<select name="dsmc" id="dsmc">
<foreach name="list1" item="vo">
<option value="{$vo.id}">{$vo.mingcheng}</option>
</foreach>
</select>
<span class="form-required">*</span>
</td>
</tr>
</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后端返回栋舍

 //获取圈舍 级联菜单
public function getjuanshe(){ $id=I('request.id');
$juanshe_model=M("Juanshe"); $list = $juanshe_model
->field("id, mingcheng")
->where("yzcid=".$id)
->order("id ASC")
->select(); //$this->ajaxReturn($list,"JSON");
echo json_encode($list);
}
05-13 08:26