JavaScript開發三級連動之核心JS

本章介紹的就是最關鍵的JS程式碼了,不多說,先看程式碼:

<script>
    //声明省
    var oProc = ["安徽","上海","山东"];  //直接声明array
    //声明市
    var oCity = [
        ["合肥","淮南","芜湖"],
        ["浦东","闵行","浦西"],
        ["济南","青岛","枣庄"]
    ];
    //声明区
    var oDist = [
        [
            ["政务区","庐阳区","蜀山区"],
            ["田家庵区","大通区","九龙岗区"],
            ["镜湖区","鸠江区","三山区"]
        ],
        [
            ["浦东1","浦东2","浦东3"],
            ["闵行1","闵行2","闵行3"],
            ["浦西1","浦西","浦西3"]
        ],
        [
            ["历下区","天桥区","长清区"],
            ["市南区","市北区","李沧区"],
            ["薛城区","市中区","峄城区"]
        ]
    ];
    var oproc = document.getElementById("proc");
    var ocity = document.getElementById("city");
    var odist = document.getElementById("dist");
    
    window.onload = function(){
        for(var i =0;i<oProc.length;i++){
            //创建元素节点
            var oOpt = document.createElement("option");
            //创建文本节点
            var oTxt = document.createTextNode(oProc[i]);
            oOpt.appendChild(oTxt);
            oproc.appendChild(oOpt);
        }
    };
    function showCity(){
        if(oproc.value=="-1"){
            ocity.options.length = 1;
            odist.options.length = 1;
        }else{
            ocity.options.length = 1;
            odist.options.length = 1;
            var num = oproc.options.selectedIndex;
            //console.log(num); 测试是否成功
            for(var i =0;i<oCity[num-1].length;i++){ 
                var oOpt = document.createElement("option");
                var oTxt = document.createTextNode(oCity[num-1][i]);
                oOpt.appendChild(oTxt);
                ocity.appendChild(oOpt);
            }
        }
    }
    function showDist(){
        if(ocity.value=='-1'){
            odist.options.length = 1
        }else{
            odist.options.length = 1;
            var numPro = oproc.options.selectedIndex;
            var numCity = ocity.options.selectedIndex;
            for(var i=0;i<oDist[numPro-1][numCity-1].length;i++){
                var oOpt = document.createElement("option");
                var oTxt = document.createTextNode(oDist[numPro-1][numCity-1][i]);
                oOpt.appendChild(oTxt);
                odist.appendChild(oOpt);
            }
        }
    }
</script>

for(var i =0;i<oCity[num-1].length;i++)這個地方有點複雜,可能會想不明白,解釋一下:[num-1]是什麼意思,為什麼要減1呢?

我們用console.log(num)測試一下,得到的結果是1,2,3,而我們陣列的起始值是0,所以要減1,。比方說,我們選擇安徽,安徽在數組中排第一個,但是起始下標是0,我們用

selectedIndex獲取的值是1,所以,想取到安徽,只能先減1.


繼續學習
||
<script> //声明省 var oProc = ["安徽","上海","山东"]; //直接声明array //声明市 var oCity = [ ["合肥","淮南","芜湖"], ["浦东","闵行","浦西"], ["济南","青岛","枣庄"] ]; //声明区 var oDist = [ [ ["政务区","庐阳区","蜀山区"], ["田家庵区","大通区","九龙岗区"], ["镜湖区","鸠江区","三山区"] ], [ ["浦东1","浦东2","浦东3"], ["闵行1","闵行2","闵行3"], ["浦西1","浦西","浦西3"] ], [ ["历下区","天桥区","长清区"], ["市南区","市北区","李沧区"], ["薛城区","市中区","峄城区"] ] ]; var oproc = document.getElementById("proc"); var ocity = document.getElementById("city"); var odist = document.getElementById("dist"); window.onload = function(){ for(var i =0;i<oProc.length;i++){ //创建元素节点 var oOpt = document.createElement("option"); //创建文本节点 var oTxt = document.createTextNode(oProc[i]); oOpt.appendChild(oTxt); oproc.appendChild(oOpt); } }; function showCity(){ if(oproc.value=="-1"){ ocity.options.length = 1; odist.options.length = 1; }else{ ocity.options.length = 1; odist.options.length = 1; var num = oproc.options.selectedIndex; //console.log(num); 测试是否成功 for(var i =0;i<oCity[num-1].length;i++){ var oOpt = document.createElement("option"); var oTxt = document.createTextNode(oCity[num-1][i]); oOpt.appendChild(oTxt); ocity.appendChild(oOpt); } } } function showDist(){ if(ocity.value=='-1'){ odist.options.length = 1 }else{ odist.options.length = 1; var numPro = oproc.options.selectedIndex; var numCity = ocity.options.selectedIndex; for(var i=0;i<oDist[numPro-1][numCity-1].length;i++){ var oOpt = document.createElement("option"); var oTxt = document.createTextNode(oDist[numPro-1][numCity-1][i]); oOpt.appendChild(oTxt); odist.appendChild(oOpt); } } } </script>