This time I will bring you ajax to realize three-level linkage (with code). What are the precautions for ajax to realize three-level linkage? . Here is a practical case, let’s take a look.
1. First place a p on a page to facilitate future reference methods
<body> <p id="sanji">//p的id为“sanji” </p>
2.sanji js processing page
$(document).ready(function(){ //向p里面放三个下拉菜单 var str = "<select id='sheng'></select> <select id='shi'></select> <select id='qu'></select>"; //给三个下拉列表定义 str 变量 $("#sanji").html(str); FillSheng(); FillShi(); FillQu();//Sheng Shi Qu的逻辑顺序 $("#sheng").change(function(){ FillShi(); FillQu(); })//给sheng菜单添加点击事件 $("#shi").change(function(){ FillQu(); })//给shi菜单添加点击事件 });//页面加载完成之后过来执行某些代码
3. Fill in the sheng method
function FillSheng() { var pcode = "";//定义一个变量 $.ajax({ url:"chuli.php", data:{pcode:pcode}, type:"POST", dataType:"TEXT", success:function(data){ var hang = data.split("^"); str +="<option value='"+lie[0]+"'>"+lie[1]+"</option>"; } $("#sheng").html(str); }); }
2.Fill shi method
function FillShi() { var pcode = $("#sheng").val(); $.ajax({ async:false, url:"chuli.php", data:{pcode:pcode}, type:"POST", dataType:"TEXT", success: function(data){ var hang = data.split("|"); var str = ""; for(var i=0;i<hang.length;i++) { var lie = hang[i].split("^"); str += "<option value='"+lie[0]+"'>"+lie[1]+"</option>"; } $("#shi").html(str); } }); }
3.Fill qu method
function FillQu() { var pcode = $("#shi").val(); $.ajax({ url:"chuli.php", data:{pcode:pcode}, type:"POST", dataType:"TEXT", success: function(data){ var hang = data.split("|"); var str = ""; for(var i=0;i<hang.length;i++) { var lie = hang[i].split("^"); str += "<option value='"+lie[0]+"'>"+lie[1]+"</option>"; } $("#qu").html(str); } }); }
4.chuli page
<?php include("DBDA.class.php"); $db = new DBDA(); $pcode = $_POST["pcode"]; $sql = "select * from chinastates where parentareacode='{$pcode}'"; echo $db->StrQuery($sql);
I believe you have mastered the method after reading the case in this article, more Please pay attention to other related articles on the php Chinese website!
Recommended reading:
How does ajax make a partial page jump function
ajax.load() method in jQuery How to use
The above is the detailed content of Ajax realizes three-level linkage (with code). For more information, please follow other related articles on the PHP Chinese website!