jquery车牌号自定义虚拟键盘

做这个APP的时候有一个输入车牌号的需求,虽然没有明确,但是我还是觉得可能会需要做成那种车牌号自定义的虚拟键盘,我试了几次,最后找到一个我最喜欢的

这是原创作者的链接,分享给大家:
https://www.cnblogs.com/xhrr/p/11557020.html

在这里我也就不像这博主一样作细分了。直接一键搞定吧,还有其他事情要做

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width,user-scalable=1.0,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,minimal-ui">
    <meta name="format-detection" content="telephone=no">
    <meta name="screen-orientation" content="portrait"><!-- uc强制竖屏 -->
    <meta name="x5-orientation" content="portrait"><!-- QQ强制竖屏 -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <title>Document</title>
    <script src="./jquery.js"></script>
    <style>
        html,
        body,
        header,
        div,
        h1,
        h2,
        h3,
        h4,
        form,
        input,
        p,
        img,
        ul,
        li,
        a,
        span,
        i,
        strong {
            padding: 0;
            margin: 0;
        }

        html,
        body {
            font-size: 50px;
            -ms-text-size-adjust: 100%;
            -webkit-text-size-adjust: 100%;
            font-family: 'Microsoft YaHei';
            line-height: 1;
            min-width: 320px;
            background-color: #f0f0f0;
        }

        ul,
        li {
            list-style: none;
        }

        i {
            font-style: normal;
        }

        a {
            text-decoration: none;
            color: #333;
        }

        img {
            border: 0;
        }

        input {
            outline: none;
            border: none;
        }

        /* header部分 */
        header {
            width: 100%;
            font-size: 0;
            text-align: center;
            font-family: 'Microsoft YaHei';
        }

        header .top {
            width: 100%;
            height: 1.28rem;
            background-color: #1c1c20;
        }

        header h1 img {
            width: 100%;
            height: 2.8rem;
        }

        section {
            padding: 0 0.4rem;
            font-size: 0;
            font-family: 'Microsoft YaHei';
        }

        .plate {
            height: 1.1rem;
            margin-top: -0.4rem;
            overflow: hidden;
        }

        .plate ul {
            height: 1.1rem;
            display: flex;
            justify-content: center;
        }

        .plate li {
            width: 0.76rem;
            height: 1.06rem;
            line-height: 1.06rem;
            border: 0.02rem solid #eee;
            margin-right: -0.02rem;
            background-color: #fff;
            font-size: 0.36rem;
            text-align: center;
        }

        .plate .active {
            border: 0.02rem solid #1aad19;
            z-index: 99;
        }

        /* 新能源车牌号 */
        /* .plate li:nth-last-child(1) {
     font-size: 0.36rem;
     height: 1.06rem;
 } */
        .plate li .new {
            color: #888;
            /* display: none; */
        }

        .plate li:nth-last-child(1) span {
            display: block;
            width: 0.26rem;
            height: 0.26rem;
            line-height: 0.2rem;
            font-size: 0.2rem;
            border: 0.01rem solid #888;
            border-radius: 50%;
            margin: 0.24rem auto 0;
        }

        .plate li:nth-last-child(1) i {
            display: block;
            font-size: 0.2rem;
            line-height: 0.4rem;
            margin-bottom: 0.2rem;
        }

        .plate li:nth-child(2) {
            margin-right: 0.4rem;
        }

        footer {
            width: 100%;
            font-size: 0;
            font-family: 'Microsoft YaHei';
        }

        /* 键盘 */
        .keyboard {
            /* height: 5.4rem; */
            background-color: #eceef1;
            position: absolute;
            bottom: 0;
            /* display: none; */
        }

        .keyboard .shut {
            height: 0.54rem;
            line-height: 0.54rem;
            text-align: right;
            padding: 0 0.16rem;
            font-size: 0.28rem;
            background-color: #f9f9f9;
            color: #333;
            border-bottom: 0.01rem solid #ddd;
            border-top: 0.01rem solid #eee;
        }

        .keyboard ul {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            padding: 0.16rem 0.08rem 0;
        }

        .keyboard li {
            /* width: 0.66rem; */
            width: 9%;
            height: 0.96rem;
            line-height: 0.96rem;
            text-align: center;
            border-radius: 0.16rem;
            background-color: #fff;
            font-size: 0.32rem;
            color: #333;
            margin: 0 0.08rem 0.16rem 0;
            box-shadow: 0 0.04rem 0 #aaa;
        }

        .keyboard li:nth-child(10n) {
            margin-right: 0;
        }

        .keyboard li:nth-last-child(1) {
            width: 1.04rem;
        }

        /* 地区简称的键盘 */
        .area {
            width: 100%;
            background-color: #eceef1;
            position: absolute;
            bottom: 0;
            display: none;
        }

        .area .shut {
            height: 0.54rem;
            line-height: 0.54rem;
            text-align: right;
            padding: 0 0.16rem;
            font-size: 0.28rem;
            background-color: #f9f9f9;
            color: #333;
            border-bottom: 0.01rem solid #ddd;
            border-top: 0.01rem solid #eee;
        }

        .area ul {
            padding: 0.16rem 0.08rem 0;
        }

        .area .area-one,
        .area .area-tow,
        .area .area-three,
        .area .area-four {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
        }

        .area li {
            /* width: 0.66rem; */
            width: 9%;
            height: 0.96rem;
            line-height: 0.96rem;
            text-align: center;
            border-radius: 0.16rem;
            background-color: #fff;
            font-size: 0.32rem;
            color: #333;
            margin: 0 0.08rem 0.16rem 0;
            box-shadow: 0 0.04rem 0 #aaa;
        }

        .area li:nth-child(10n) {
            margin-right: 0;
        }

        /* .area .area-four li:nth-last-child(1) {
     width: 1.04rem;
 } */

        /* 手机屏幕小于370px的屏幕 */
        @media screen and (min-width: 450px) {

            .keyboard li,
            .area li {
                width: 0.66rem;
            }
        }

        /* 手机屏幕小于370px的屏幕 */
        @media screen and (max-width: 370px) {

            .keyboard li,
            .area li {
                /* width: 0.54rem; */
                width: 8%;
                height: 0.8rem;
                line-height: 0.8rem;
            }

            .plate li:nth-child(2) {
                margin-right: 0.1rem;
            }

            .plate li {
                width: 0.66rem;
            }

            .plate li:nth-last-child(1) span {
                display: block;
                line-height: 0.2rem;
                margin: 0.1rem auto 0;
            }

            .plate li:nth-last-child(1) i {
                display: block;
                font-size: 0.2rem;
                line-height: 0.32rem;
                /* margin-bottom: 0.2rem; */
            }
        }

        /* 查询缴费 */
        section .binding {
            width: 100%;
            height: 0.8rem;
            line-height: 0.8rem;
            margin-top: 1.04rem;
            text-align: center;
            font-size: 0.40rem;
            color: #fff;
            border-radius: 0.16rem;
            background-color: #1aad19;
            /* opacity: 0.5; */
        }

        /* 不足15分钟 */
        section .plate-not {
            font-size: 0.32rem;
            line-height: 1.6rem;
            text-align: center;
            color: #888;
        }

        /* 弹出框背景变灰色 */
        .poplayer3 {
            width: 100%;
            height: 100%;
            position: absolute;
            display: block;
            left: 0;
            top: 0;
            z-index: 99;
            background-color: #333;
            -moz-opacity: 0.6;
            opacity: 0.6;
            filter: alpha(opacity=60);
            display: none;
        }

        /*  缴费详情 */
        .detail3 {
            width: 5.42rem;
            height: 7.68rem;
            background-color: #fff;
            color: #333;
            border-radius: 0.16rem;
            left: 50%;
            top: 50%;
            margin-left: -2.95rem;
            margin-top: -3.68rem;
            position: fixed;
            z-index: 100;
            display: none;
            padding: 0 0.24rem;
        }

        .detail3 strong {
            width: 0.56rem;
            height: 0.56rem;
            line-height: 0.56rem;
            border: 0.01rem solid #e64340;
            border-radius: 50%;
            text-align: center;
            position: absolute;
            top: 0.12rem;
            right: 0.12rem;
            font-size: 0.32rem;
            color: #e64340;
            text-align: center;
        }

        .detail3 h3 {
            font-size: 0.36rem;
            text-align: center;
            margin-top: 0.48rem;
        }

        /* 缴费信息 */
        .detail3 ul {
            margin: 0.8rem 0.24rem 0 0.24rem;
            border-bottom: 0.01rem solid #ccc;
            padding-bottom: 0.48rem;
        }

        .detail3 li span {
            font-size: 0.28rem;
            line-height: 0.48rem;
            color: #666;
        }

        .detail3 li i {
            font-size: 0.28rem;
            line-height: 0.48rem;
            margin-left: 1.2rem;
        }

        .detail3 .wechat {
            width: 100%;
            height: 0.8rem;
            line-height: 0.8rem;
            margin-top: 0.8rem;
            text-align: center;
            font-size: 0.40rem;
            color: #fff;
            border-radius: 0.16rem;
            background-color: #1aad19;
            /* opacity: 0.5; */
        }

        .detail3 .alipay {
            width: 100%;
            height: 0.8rem;
            line-height: 0.8rem;
            margin-top: 0.4rem;
            text-align: center;
            font-size: 0.40rem;
            color: #fff;
            border-radius: 0.16rem;
            background-color: #108ee9;
            /* opacity: 0.5; */
        }

        /* 缴费成功 */
        .payment {
            margin: 0 auto;
            background-color: #fff;
            border-radius: 0.16rem;
            margin-top: 0.24rem;
            padding: 0.24rem;
        }

        /* 缴费信息 */
        .payment ul {
            margin: 0.48rem 0.4rem 0 0.4rem;
            border-bottom: 0.01rem solid #ccc;
            padding-bottom: 0.48rem;
        }

        .payment h4 {
            font-size: 0.32rem;
            line-height: 0.8rem;
            color: #666;
        }

        .payment h4 em {
            font-style: normal;
            font-size: 0.4rem;
            color: #e64340;
        }

        .payment strong {
            font-size: 0.28rem;
            line-height: 0.48rem;
            color: #e64340;
        }

        .payment li span {
            font-size: 0.28rem;
            line-height: 0.48rem;
            color: #666;
        }

        .payment li i {
            font-size: 0.28rem;
            line-height: 0.48rem;
            margin-left: 0.8rem;
        }

        .payment p {
            /* text-align: right; */
            font-size: 0.24rem;
            line-height: 0.48rem;
            color: #aaa;
        }
    </style>
</head>

<body>
    <header>
        <div class="top"></div>
    </header>
    <section style="margin-top: 40px;">
        <div class="plate">
            <ul id="plate">
                <li>粤</li>
                <li>B</li>
                <li class="active"></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li>
                    <div class="new">
                        <span>+</span>
                        <i>新能源</i>
                    </div>
                </li>
            </ul>
        </div>

    </section>

    <footer>
        <!-- 键盘 -->
        <div class="keyboard" id="shutkey">
            <div class="shut" id="shut">关闭</div>
            <ul id="keyboard">
                <li>1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
                <li>5</li>
                <li>6</li>
                <li>7</li>
                <li>8</li>
                <li>9</li>
                <li>0</li>

                <li>Q</li>
                <li>W</li>
                <li>E</li>
                <li>R</li>
                <li>T</li>
                <li>Y</li>
                <li>U</li>
                <li>P</li>
                <li>港</li>
                <li>澳</li>

                <li>A</li>
                <li>S</li>
                <li>D</li>
                <li>F</li>
                <li>G</li>
                <li>H</li>
                <li>J</li>
                <li>K</li>
                <li>L</li>
                <li>学</li>

                <li>Z</li>
                <li>X</li>
                <li>C</li>
                <li>V</li>
                <li>B</li>
                <li>N</li>
                <li>M</li>
                <li>警</li>
                <li>删除</li>
            </ul>
        </div>
        <!-- 地区车牌简称 -->
        <div class="area" id="area">
            <div class="shut" id="shuta">关闭</div>
            <ul id="keyboarda">
                <div class="area-one">
                    <li>京</li>
                    <li>津</li>
                    <li>沪</li>
                    <li>渝</li>
                    <li>蒙</li>
                    <li>新</li>
                    <li>藏</li>
                    <li>宁</li>
                    <li>桂</li>
                    <li>黑</li>
                </div>
                <div class="area-tow">
                    <li>吉</li>
                    <li>辽</li>
                    <li>晋</li>
                    <li>冀</li>
                    <li>青</li>
                    <li>鲁</li>
                    <li>豫</li>
                    <li>苏</li>
                    <li>皖</li>
                </div>
                <div class="area-three">
                    <li>浙</li>
                    <li>闽</li>
                    <li>赣</li>
                    <li>湘</li>
                    <li>鄂</li>
                    <li>粤</li>
                    <li>琼</li>
                    <li>甘</li>
                </div>
                <div class="area-four">
                    <li>陕</li>
                    <li>贵</li>
                    <li>云</li>
                    <li>川</li>
                    <!-- <li>删除</li> -->
                </div>
            </ul>
        </div>
    </footer>
    <script>

        window.onload = function () {

            // 初始化rem单位
            (function () {
                document.addEventListener('DOMContentLoaded', function () {
                    function htmlsize() {
                        let html = document.documentElement;
                        let windowWidth = html.clientWidth;
                        if (windowWidth <= 750) {
                            html.style.fontsize = windowWidth / 7.5 + 'px';
                            // 等价于html.style.fontSize = windowWidth / 750 * 100 + 'px';
                        } else {
                            html.style.fontSize = 100 + 'px';
                        }
                    }
                    htmlsize();
                    window.onresize = function () {
                        htmlsize()
                    }
                }, false)
            })();


            (function ($) {
                // 获取键盘上的关闭按钮
                var oShut = $("#shut");
                // 获取键盘的id
                var oShutkey = $("#shutkey");
                // 获取车牌框切换的id
                var oPlate = $("#plate");
                // 获取号码牌的li
                var oPli = $("#plate li");
                // 获取数字拼音软键盘的每个值
                var oOli = $("#keyboard li");
                // 获取地名简称软键盘的每个值
                var oOlia = $("#keyboarda li");
                // 获取地方简称键盘的id
                var oArea = $("#area");
                // 获取地方简称的关闭按钮
                var oShuta = $("#shuta");
                // 获取查询缴费的id
                var oBinding = $("#binding");
                // 获取显示车牌号的id
                var oPlateNum = $("#platenum");
                // 获取弹出框的id
                var oDetail3 = $("#detail3");
                // 获取小红叉的id
                var oCancelParking3 = $("#CancelParking3");
                // 获取背景灰色的id
                var oPoplayer3 = $("#poplayer3");

                // 点击关闭,关闭键盘
                oShut.click(function () {
                    oShutkey.slideUp(100);
                });
                // 点击关闭,关闭地名键盘
                oShuta.click(function () {
                    oArea.slideUp(100);
                });

                // 切换车牌号码框的函数
                (function () {
                    // 定义车牌框变量
                    var oPok = 2;
                    // 定义键盘变量
                    var oOlok;

                    // 给车牌框绑定点击事件
                    oPli.bind("click", function () {
                        // 获取车牌框里面的索引值
                        var index = $(this).index();
                        // 点击哪个框就给哪个框添加绿色框,并去掉其他绿色框
                        $(this).addClass("active").siblings().removeClass("active");
                        oPok = index;
                        // 隐藏新能源信息
                        function onew() {
                            $(".new").show(50);
                        };
                        // 隐藏地名简称键盘
                        function oarea() {
                            oArea.slideUp(200);
                        };
                        // 显示数字拼音键盘
                        function oshutkey() {
                            oShutkey.slideDown(200);
                        };

                        // 当点击第一个的时候,调出地名简称键盘
                        if (oPok === 0) {
                            onew();
                            oArea.slideDown(200);
                            oShutkey.slideUp(200);
                            // 当点击最后一个的时候隐藏自己的innerText值
                        } else if (oPok === 7) {
                            $(".new").hide(100);
                            oarea()
                            oshutkey()
                            // 当点击第二个的时候,弹出数字拼音键盘
                        } else if (oPok === 1) {
                            onew();
                            oarea()
                            oshutkey()
                            // 当点击第7个的时候全部显示
                        } else if (oPok === 6) {
                            onew();
                            oarea()
                            oshutkey()
                            // 当点击其他车牌框的时候调出数字拼音键盘
                        } else {
                            onew();
                            oarea()
                            oshutkey()
                        };
                    });

                    // 给数字拼音键盘绑定点击事件
                    oOli.bind("click", function () {
                        // 获取键盘的索引
                        var index = $(this).index();
                        oOlok = index;
                        // 绿色框加到下一个
                        function opli() {
                            oPli.eq(oPok).addClass("active").siblings().removeClass("active");
                        };
                        // 点击删除按钮,往后回删内容
                        if (oOlok === 38) {
                            oPli.eq(oPok).html("");
                            oPok--;
                            opli();
                            // 当回删到第一个的时候隐藏数字拼音键盘,显示地名简称键盘
                            if (oPok === 0) {
                                oArea.slideDown(200);
                                oShutkey.slideUp(200);
                                //
                            } else if (oPok < 8) {
                                oPli.eq(7).html('<div class="new"><span>+</span><i>新能源</i></div>');
                            };
                        } else {
                            // 点击数字拼音键盘替换获取车牌框索引值的值
                            oPli.eq(oPok).html(this.innerText);
                            // 每点击一次增加一次索引
                            oPok++;
                            // 每点击一次绿色框跳转到下一个
                            opli();
                        }
                        // 当绿色框达到第7个的时候隐藏绿色框,并隐藏键盘
                        if (oPok > 6) {
                            oShutkey.slideUp(200);
                            oPli.removeClass("active");
                        };
                    });

                    // 给地名键盘绑定点击事件
                    oOlia.bind("click", function () {
                        // 点击简称键盘获取车牌索引值的值
                        oPli.eq(oPok).html(this.innerText);
                        // 点击一次增加一次索引
                        oPok++;
                        if (oPok === 1) {
                            oArea.slideUp(200);
                            oShutkey.slideDown(200);
                        };
                        // 点击一次绿色框跳转到下一个
                        oPli.eq(oPok).addClass("active").siblings().removeClass("active");
                    });

                    // 点击查询缴费弹出框,并获取输入的车牌号
                    oBinding.click(function () {
                        var oStr;
                        // 判断车牌最后一位长度是否大于1
                        if (oPli.eq(7).text().length > 1) {
                            // 如果大于1,显示前7位
                            oStr = $("#plate li:lt(7)").text();
                            oPlateNum.text(oStr);
                        } else {
                            // 最后一位输入车牌就显示8位
                            oStr = $("#plate li:lt(8)").text();
                            oPlateNum.text(oStr);
                        };
                        // 显示弹框
                        oDetail3.show(100);
                        // 显示背景层
                        oPoplayer3.show();
                    });
                })();

                // 点击小红叉隐藏页面
                oCancelParking3.click(function () {
                    oDetail3.hide(100);
                    oPoplayer3.hide();
                });
            })(jQuery);
        }
    </script>
</body>

</html>

以上就是jquery车牌号自定义虚拟键盘的解决方法了
如果这篇文章对你有帮助,或者在进行中遇到其他问题,欢迎评论区留言出来。
我们一起探讨~

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容