PHP+jQuery無限極分類
阿新 • • 發佈:2019-01-09
<select class="sel"> {volist name="arr" id="v"} <option value="{$v.id}">{$v.name}</option> {/volist} </select> <script> $(document).on('change','.sel',function(){ //定義一個id var id=$(this).val(); var obj=$(this); // alert(obj);die; $.get("{:url('Index/pid')}",{id:id},function(data){ //刪除後面所有的 obj.nextAll().remove(); //轉化為內建物件 var json=jQuery.parseJSON(data); //判斷他沒有找到的話,就false掉 if(json==''){ return false; } var sel="<select class='sel'>"; $(json).each(function(key,val){ sel+="<option value='"+val.id+"'>"+val.name+"</option>" }); sel+="</select>"; //後面追加 obj.after(sel); }) }) </script>
<?php namespace app\index\controller; use app\index\model\Index as u; use think\Controller; use think\Db; use think\Request; class index extends Controller{ public function index(){ $res=Db::table('attr')->where('pid=0')->select(); return view('index',['arr'=>$res]); } public function pid(){ $id=Request::instance()->get('id'); $arr=Db::table('attr')->where('pid',$id)->select(); return json_encode($arr); } }