纯JS使用switch和if语句判断控制卡的型号

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>

<select name="" id="type">
    <option value="U盘卡">U盘卡</option>
    <option value="WIFI卡">WIFI卡</option>
    <option value="网口卡">网口卡</option>
</select>

<button id="bt">按钮</button>

<script>

        //长度值
        width = 640;
        height = 16;
        type = "";      //最终打印出来的型号
        //U盘系列 boolean变量
        UN = width <= 320 && height<= 32 || width<=640 && height<=16;
        UM = width <= 512 && height<= 32 || width<=1024 && height<=16;




        document.getElementById("bt").onclick = function(){
            //型号选择
            var obj = document.getElementById("type");
            var  index = obj.selectedIndex;
            var tp =  obj[index].text;  //选择的型号

            switch(tp){
                case "U盘卡":
                    f1();
                    break;
                case "WIFI卡":
                    f2();
                    break;
                case "网口卡":
                    f3();
                    break;
            }

            //选择到那个型号,就调用指定型号的方法
            function f1(){
                if (UN) {
                    type = "ZH-UN";
                } else if (UM) {
                    type = "ZH-UM";
                }
                alert(type);
            }

            //选择到那个型号,就调用指定型号的方法
            function f2(){
                if(UN){
                    type = "ZH-WN";
                }else if(UM){
                    type = "ZH-WM";
                }
                alert(type);
            }

            //选择到那个型号,就调用指定型号的方法
            function f3(){
                if(UN){
                    type = "ZH-E1";
                }else if(UM){
                    type = "ZH-E2";
                }
                alert(type);
            }
        }

</script>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/byte_dance/article/details/79774073