<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);
}
}