首頁  >  文章  >  web前端  >  ajax實現三級連動寫法的實例分析

ajax實現三級連動寫法的實例分析

黄舟
黄舟原創
2017-10-30 10:36:431093瀏覽

主頁面代碼

<!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: &#39;post&#39;,   //发送请求的方法(get)        
        url: &#39;sheng_l.php&#39;,//发送请求的地址        
        success: function (data) {//发送成功时的回调函数
    //            console.log(data);
            for (var i in data) {
                $("#sheng").append("<option value=&#39;"+ data[i][2] +"&#39;>" + 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=&#39;" + data[i][2] +"&#39;>" + 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=&#39;" +data[i][2] +"&#39;>" +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[&#39;parent_id&#39;]}";$result = $db->Query($sql);echo json_encode($result);

以上是ajax實現三級連動寫法的實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn