ThinkPHP+AJAX三级联动
生活随笔
收集整理的這篇文章主要介紹了
ThinkPHP+AJAX三级联动
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
sanji.html
<!doctype html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><load href ="__PUBLIC__/JQ/jquery-1.12.4.min.js"/> </head> <body><h1>三級聯(lián)動</h1> <div id='sanji'> <select id = 'sheng'></select><select id ='shi'></select><select id = 'xian'></select> </div> </body> </html> <script>$(document).ready(function(e){sheng(); shi(); xian();//省級列表數(shù)據(jù)function sheng(){$.ajax({url:"__CONTROLLER__/sheng",dataType:"json", async:false, //同步 success:function(sheng){var str ='';for(var i=0;i<sheng.length;i++){str += "<option value = " + sheng[i].pro_id +">"+ sheng[i].pro_name + "</option>";}$("#sheng").html(str); //填充省級列表 }});}//市級列表數(shù)據(jù)function shi(){var id = $("#sheng").val();$.ajax({url:"__CONTROLLER__/shi",data:{id:id},dataType:"json",async:false, //同步 type:"post",success:function(shi){var str = '';for(var i=0;i<shi.length;i++){str += "<option value=" + shi[i].city_id +">"+ shi[i].city_name + "</option>";}$("#shi").html(str); //填充市級列表 }});}function xian(){var id = $("#shi").val();$.ajax({url:"__CONTROLLER__/xian",data:{id:id},dataType:"json",async:false, //同步 type:"post",success:function(xian){var str = '';for(var i=0;i<xian.length;i++){str += "<option value=" + xian[i].cou_id +">"+ xian[i].cou_name + "</option>";}$("#xian").html(str); //填充縣級列表 }});}$("#sheng").change(function(){shi();xian();});$("#shi").change(function(){xian();});});</script> View CodeSanjiController.class.php
<?php namespace Home\Controller; use Think\Controller; class SanjiController extends Controller{public function sanji(){$this->display();} //省級列表public function sheng(){$pro = M('pro');$sheng = $pro->select();$this->ajaxReturn($sheng);}//市級列表public function shi(){$id = I('id');$city = M('city');$shi = $city->where(array('pro_id'=>$id))->select();$this->ajaxReturn($shi);}public function xian(){$id = I('id');$cou = M('cou');$xian = $cou->where(array('city_id'=>$id))->select();$this->ajaxReturn($xian);}}?> View Code省級表? 市級表
?
?
縣級表
?
注:表中數(shù)據(jù)僅作為練習(xí)所用,請不要對號入座!
轉(zhuǎn)載于:https://www.cnblogs.com/sihuiming/p/5540983.html
總結(jié)
以上是生活随笔為你收集整理的ThinkPHP+AJAX三级联动的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 麦胡图玉米煮着粘吗?
- 下一篇: 如何制作健康又美味的甜点?