標籤:san 省份 3.1.1 遍曆數組 3.1 三級聯動 ajax 下拉 src
<div id="sanji"></div>
下面考慮的是要有省市區這三列,這三列用的是下拉式清單,那麼裡面要用<option></option> 因為是用js和jquery來寫,那麼首先要考慮的就是要引入jquery包和js檔案,然後把寫三個下拉式清單
<script src="jquery-3.1.1.min.js"></script>
<script src="sanji.js"></script>
$(document).ready(function(e){
var str="<select id=‘sheng‘></select><select id=‘shi‘></select><select id=‘qu‘></select>"; //先寫三個下拉式清單放到div裡面
$("#sanji").html(str);
fullsheng();
fullshi();
fullqu();
$("#sheng").change(function(){
fullshi();
fullqu();
})
$("#shi").change(function(){
fullqu();
})
//載入省份資訊
function fullsheng()
{
var pcode="0001";//根據父級代號查資料
$.ajax({
async:false, //採用非同步方式
url:"sanjichuli.php",
data:{pcode:pcode},
type:"POST",
dataType:"JSON",
success:function(data){
//這裡傳過來的data是個數組
str="";
for(var j in data)//js中的遍曆數組用for來表示
{
str +="<option value=‘"+data[j].AreaCode+"‘>"+data[j].AreaName+"</option>";
}
$("#sheng").html(str);
}
})
}
//載入市的資訊
function fullshi()
{
var pcode=$("#sheng").val();
$.ajax({
async:false,
url:"sanjichuli.php",
data:{pcode:pcode},
type:"POST",
dataType:"JSON",
success:function(data){
//這裡傳過來的data是個數組
str="";
for(var j in data)//js中的遍曆數組用for來表示
{
str +="<option value=‘"+data[j].AreaCode+"‘>"+data[j].AreaName+"</option>";
}
$("#shi").html(str);
}
})
}
// 載入區的資訊
function fullqu()
{
var pcode=$("#shi").val();
$.ajax({
url:"sanjichuli.php",
data:{pcode:pcode},
type:"POST",
dataType:"JSON",
success:function(data){
//這裡傳過來的data是個數組
str="";
for(var j in data)//js中的遍曆數組用for來表示
{
str +="<option value=‘"+data[j].AreaCode+"‘>"+data[j].AreaName+"</option>";
}
$("#qu").html(str);
}
})
}
})
未加星標 ajax三級聯動的實現方法