基于JQ的三级联动菜单选择

<!-- author:青芒 -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>三级联动菜单</title>
</head>
<body>
    <select class="one">
        <option value="请选择">请选择</option>
    </select>

    <select class="two">
        <option value="请选择">请选择</option>
    </select>

    <select class="three">
        <option value="请选择">请选择</option>
    </select>

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script>
        $(function(){
            var oneArr = ["1","2","3"]; //数组1

            var twoArr = [ //数组2
                ["1-1","1-2","1-3"],
                ["2-1","2-2","2-3"],
                ["3-1","3-2","3-3"]
            ];

            var threeArr = [ //数组3
                [
                    ["1-1-1","1-1-2","1-1-3"],["1-2-1","1-2-2","1-2-3"],["1-3-1","1-3-2","1-3-3"]
                ],
                [
                    ["2-1-1","2-1-2","2-1-3"],["2-2-1","2-2-2","2-2-3"],["2-3-1","2-3-2","2-3-3"]
                ],
                [
                    ["3-1-1","3-1-2","3-1-3"],["3-2-1","3-2-2","3-2-3"],["3-3-1","3-3-2","3-3-3"]
                ]
            ];

            let oneHtml = '<option value="请选择">请选择</option>';
            for(let i in oneArr){
                i = Number(i);
                oneHtml += '<option value="'+ i +'">'+ oneArr[i] +'</option>';
            }
            $(".one").html(oneHtml);

            $(".one").change(function(){ //下拉框1选择事件
                if($(this).val() == "请选择"){ //下拉框1如果点击了请选择,则2,3都内容都重置成“请选择”
                    $(".two, .three").html('<option value="请选择">请选择</option>');
                    return;
                }

                let thisVal = Number($(this).val());
                let twoHtml = '';
                for(let j in twoArr[thisVal]){
                    j = Number(j);
                    twoHtml += '<option value="'+ (j+1) +'">'+ twoArr[thisVal][j] +'</option>';
                }
                $(".two").html(twoHtml);

                let threeHtml = '';
                for(let k in threeArr[thisVal][0]){
                    k = Number(k);
                    threeHtml += '<option value="'+ (k+1) +'">'+ threeArr[thisVal][0][k] +'</option>';
                }
                $(".three").html(threeHtml);
            });

            $(".two").change(function(){ //下拉框2选择事件
                let oneVal = Number($(".one").val());
                let thisVal = Number($(this).val()) - 1;

                let threeHtml = '';
                for(let x in threeArr[oneVal][thisVal]){
                    threeHtml += '<option value="'+ (x+1) +'">'+ threeArr[oneVal][thisVal][x] +'</option>';
                }
                $(".three").html(threeHtml);
            });
        })
    </script>
</body>
</html>

 

转载于:https://www.cnblogs.com/muou2125/p/9212320.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值