JS製作簡單的三級聯動,

來源:互聯網
上載者:User

JS製作簡單的三級聯動,

用javascript製作的一個簡單三級聯動,非常簡單實用

複製代碼 代碼如下:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        省:
        <select style="width: 100px;" id="pre" onchange="chg(this);">
            <option value="-1">請選擇</option>
        </select>
        市:
        <select style="width: 100px;" id="city" onchange="chg2(this)" ;></select>
        區:
        <select style="width: 100px;" id="area"></select>
    </body>
    <script>
         //聲明省
        var pres = ["北京", "上海", "山東"]; //直接聲明Array
         //聲明市
        var cities = [
            ["東城", "昌平", "海澱"],
            ["浦東", "高區"],
            ["濟南", "青島"]
        ];
        var areas = [
                [
                    ["東城1", "東城2", "東城3"],
                    ["昌平1", "昌平2", "昌平3"],
                    ["海澱1", "海澱2", "海澱3"]
                ],
                [
                    ["浦東1", "浦東2", "浦東3"],
                    ["高區1", "高區2", "高區3"]
                ],
                [
                    ["濟南1", "濟南2"],
                    ["青島1", "青島2"]
                ]
            ]
            //設定一個省的公用下標
        var pIndex = -1;
        var preEle = document.getElementById("pre");
        var cityEle = document.getElementById("city");
        var areaEle = document.getElementById("area");
         //先設定省的值
        for (var i = 0; i < pres.length; i++) {
            //聲明option.<option value="pres[i]">Pres[i]</option>
            var op = new Option(pres[i], i);
            //添加
            preEle.options.add(op);
        }
        function chg(obj) {
            if (obj.value == -1) {
                cityEle.options.length = 0;
                areaEle.options.length = 0;
            }
            //擷取值
            var val = obj.value;
            pIndex = obj.value;
            //擷取ctiry
            var cs = cities[val];
            //擷取預設區
            var as = areas[val][0];
            //先清空市
            cityEle.options.length = 0;
            areaEle.options.length = 0;
            for (var i = 0; i < cs.length; i++) {
                var op = new Option(cs[i], i);
                cityEle.options.add(op);
            }
            for (var i = 0; i < as.length; i++) {
                var op = new Option(as[i], i);
                areaEle.options.add(op);
            }
        }
        function chg2(obj) {
            var val = obj.selectedIndex;
            var as = areas[pIndex][val];
            areaEle.options.length = 0;
            for (var i = 0; i < as.length; i++) {
                var op = new Option(as[i], i);
                areaEle.options.add(op);
            }
        }
    </script>
</html>

以上所述就是本文的全部內容了,希望大家能夠喜歡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.