溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ajax如何實現(xiàn)城市三級聯(lián)動

發(fā)布時間:2021-10-18 12:36:26 來源:億速云 閱讀:130 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了ajax如何實現(xiàn)城市三級聯(lián)動,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體內(nèi)容如下

在準(zhǔn)備好服務(wù)器

html部分

<div>
        <select name="" id="province">
            <option value="">請選擇省份</option>
        </select>
        <select name="" id="city">
            <option value="">請選擇城市</option>
        </select>
        <select name="" id="district">
            <option value="">請選擇區(qū)域</option>
        </select>
</div>

樣式部分

<style>
        div {
            text-align: center;
        }

        select {
            width: 150px;
            height: 30px;
        }
</style>

js部分

<script>
        var a = 0;
        var b = 0;
        var d=null;

            $.ajax({
                type:'get',
                url:'http://127.0.0.1:6562/0929/area-json.js',
                success: function(data){         
                    d = JSON.parse(data.slice(10,-1)); //獲取json數(shù)據(jù)并轉(zhuǎn)化為數(shù)組數(shù)據(jù)
                    $.each(d,function(index,ele){
                    $('<option value = ""></option>').appendTo('#province').text(ele.name);//把省的數(shù)據(jù)插入列表中
                    })
            }
            })
        $('#province').on('change',function(e){  //當(dāng)省變化時
            $.ajax({
                type:'get',
                url:'http://127.0.0.1:6562/0929/area-json.js',
                success: function(data){         
                    d=JSON.parse(data.slice(10,-1));
                    a = e.target.selectedIndex - 1;  //當(dāng)前下拉列表下標(biāo)
                    if(a == -1){
                        $('#city').html('<option value="">請選擇城市</option>');
                        $('#district').html('<option value="">請選擇區(qū)域</option>');
                    }else{
                        $('#city').html('<option value="">請選擇城市</option>');
                        $('#district').html('<option value="">請選擇區(qū)域</option>');
                        if(d[a].children){
                            $.each(d[a].children,function(index,ele){
                                $('<option value = ""></option>').appendTo('#city').text(ele.name);

                            })
                        }
                    }
            }
            })
        })
        $('#city').on('change',function(e){   //當(dāng)市變化時
            $.ajax({
                type:'get',
                url:'http://127.0.0.1:6562/0929/area-json.js',
                success: function(data){         
                    d=JSON.parse(data.slice(10,-1));
                    b = e.target.selectedIndex - 1;
                    if(b == -1){
                        $('#district').html('<option value="">請選擇區(qū)域</option>');
                    }else{
                        $('#district').html('<option value="">請選擇區(qū)域</option>');
                        if(d[a].children[b].children){
                            $.each(d[a].children[b].children,function(index,ele){
                                $('<option value = ""></option>').appendTo('#district').text(ele.name);

                            })
                        }
                    }
            }
            })
            
        })
</script>

全部代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.11.3/jquery.js"></script>
    <style>
        div {
            text-align: center;
        }

        select {
            width: 150px;
            height: 30px;
        }
    </style>
</head>
<body>
    <div>
        <select name="" id="province">
            <option value="">請選擇省份</option>
        </select>
        <select name="" id="city">
            <option value="">請選擇城市</option>
        </select>
        <select name="" id="district">
            <option value="">請選擇區(qū)域</option>
        </select>
    </div>
    <script>
        var a = 0;
        var b = 0;
        var d=null;

            $.ajax({
                type:'get',
                url:'http://127.0.0.1:6562/0929/area-json.js',
                success: function(data){         
                    d = JSON.parse(data.slice(10,-1));
                    $.each(d,function(index,ele){
                    $('<option value = ""></option>').appendTo('#province').text(ele.name);
                    })
            }
            })
        $('#province').on('change',function(e){
            $.ajax({
                type:'get',
                url:'http://127.0.0.1:6562/0929/area-json.js',
                success: function(data){         
                    d=JSON.parse(data.slice(10,-1));
                    a = e.target.selectedIndex - 1;
                    if(a == -1){
                        $('#city').html('<option value="">請選擇城市</option>');
                        $('#district').html('<option value="">請選擇區(qū)域</option>');
                    }else{
                        $('#city').html('<option value="">請選擇城市</option>');
                        $('#district').html('<option value="">請選擇區(qū)域</option>');
                        if(d[a].children){
                            $.each(d[a].children,function(index,ele){
                                $('<option value = ""></option>').appendTo('#city').text(ele.name);

                            })
                        }
                    }
            }
            })
        })
        $('#city').on('change',function(e){
            $.ajax({
                type:'get',
                url:'http://127.0.0.1:6562/0929/area-json.js',
                success: function(data){         
                    d=JSON.parse(data.slice(10,-1));
                    b = e.target.selectedIndex - 1;
                    if(b == -1){
                        $('#district').html('<option value="">請選擇區(qū)域</option>');
                    }else{
                        $('#district').html('<option value="">請選擇區(qū)域</option>');
                        if(d[a].children[b].children){
                            $.each(d[a].children[b].children,function(index,ele){
                                $('<option value = ""></option>').appendTo('#district').text(ele.name);

                            })
                        }
                    }
            }
            })
            
        })
    </script>
</body>
</html>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“ajax如何實現(xiàn)城市三級聯(lián)動”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI