php 代码:
- public function liandong(){ $arr = Db::table("city")->where("pid=0")->select();
- return view('liandong',['arr'=>$arr]);
- }
- // 输出联动 json
- public function liandong_json(){
- $pid = Request::instance()->param('pid');
- $arr = Db::table("city")->where("pid='$pid'")->select();
- echo json_encode($arr);
- }
html 代码:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- </head>
- <body>
- <select id="one">
- {volist name="arr" id="v"}
- <option value="{$v.id}">{$v.c_name}</option>
- {/volist}
- </select>
- <select id="two">
- <option value="北京"> 北京 </option>
- </select>
- <script src="__STATIC__/js/jquery.js"></script>
- <script>
- $(document).on('change','#one',function(){
- var pid = $(this).val();
- //ajax 请求后台数据
- $.get("{:url('Two/liandong_json')}?pid="+pid,function(data){
- var res = JSON.parse(data);
- var str = "";
- // <option value='-- 请选择 --' class='two'>-- 请选择 --</option>
- $.each(res,function(k,v){
- str += "<option value='"+v.id+"'>"+v.c_name+"</option>";
- })
- $("#two").empty();
- $("#two").append(str);
- })
- });
- </script>
- </body>
- </html>
来源: http://www.bubuko.com/infodetail-2766122.html