简单实现ajax三级联动效果
本文实例为大家分享了ajax三级联动效果展示的具体代码,供大家参考,具体内容如下
主页面代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../wenjian/jquery-2.2.3.min.js"></script> </head> <body> <select id="sheng"> <option>请选择</option> </select> <select id="shi"> <option >请选择</option> </select> <select id="qu"> <option >请选择</option> </select> </body> </html> <script> $.ajax({ data: {parent_id: 0}, //发送的数据 dataType: "json", //返回值的类型 text为string型 type: 'post', //发送请求的方法(get) url: 'sheng_l.php',//发送请求的地址 success: function (data) {//发送成功时的回调函数 // console.log(data); for (var i in data) { $("#sheng").append("<option value='"+ data[i][2] +"'>" + data[i][1] +"</option>") } } }) $(document).ready(function () { $("#sheng").change(function () { $("#shi").get(0).options.length= 1; // $("#qu").get(0).options.length= 1; var data = $("#sheng").find("option:selected").val(); $.ajax({ data:{parent_id:data}, type:"post", dataType:"json", url:"sheng_l.php", success:function (data) { for(var i in data){ $("#shi").append("<option value='" + data[i][2] +"'>" + data[i][1] +"</option>") } } }) }) }) $(document).ready(function () { $("#shi").change(function () { $("#qu").get(0).options.length= 1; var data = $("#shi").find("option:selected").val(); $.ajax({ data:{parent_id:data}, type:"post", dataType:"json", url:"sheng_l.php", success:function (data) { for (var i in data){ $("#qu").append("<option value='" +data[i][2] +"'>" +data[i][1] +"</option>") } } }) }) })
处理页面代码
<?php /** * Created by fcc * User: Administrator * Date: 2017/10/29 * Time: 20:56 */ require_once "../wenjian/DBDA.class.php"; $db = new DBDA(); $sql = "select * from region WHERE father_id = {$_POST['parent_id']}"; $result = $db->Query($sql); echo json_encode($result);
相关推荐
时光如瑾雨微凉 2020-07-19
ppsurcao 2020-06-14
李永毅 2020-06-21
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07