jQuery虚拟键盘正在保存先前输入的值

时间:2019-05-30 06:21:41

标签: javascript jquery html

我使用虚拟键盘,但遇到了问题。

问题是,在一个用户界面中,我只有几个输入字段,对此每个字段我都有类“输入” ,称为键盘。

当我点击进行首次输入并输入数据时,该用于先前输入字段的数据将复制到新数据。

$(document).ready(function () {
    $(".numpad").hide();
    $('.input').click(function () {
        $('.numpad').fadeToggle('fast');
    });

    $('.del').click(function () {
        $('.input').val($('.input').val().substring(0, $('.input').val().length - 1));
    });
    $('.faq').click(function () {
        alert("It's virtual keyboard");
    })
    $('.shuffle').click(function () {
        $('.input').val($('.input').val() + $(this).text());
    });

});

$(".input").on("click", function () {
    $('.modal, .cover').removeClass("hidden");
    $('.modal').addClass("zoom");
});

//hide modal
$(".cover, .close").on("click", function () {
    $('.modal').attr('class', 'modal');
    $('.modal, .cover').addClass("hidden");
});
.keyBoardButton {
    margin: 13px;
}

.keyBoardButton {
    display: inline-block;
    border: 1px solid #fff;
    color: #fff;
    border-radius: 30px;
    -webkit-border-radius: 30px;
    -moz-border-radius: 30px;
    font-family: Verdana;
    width: auto;
    height: auto;
    font-size: 22px;
    padding: 20px 27px;
    background-color: #878787;
}

    .keyBoardButton:hover, .keyBoardButton:active {
        border: 1px solid #FFFFFF;
        color: #000;
        background-color: #fff;
    }

.cover {
    z-index: 1;
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: #333;
    top: 0;
    left: 0;
    opacity: .9;
}

.modal {
    z-index: 2;
    height: 390px;
    width: 350px;
    background-color: #262626;
    border-radius: 5px;
    text-align: center;
    border-top: solid 3px #262626;
    position: absolute;
    opacity: 0.9;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}

    .modal .content p {
        font-size: 2em;
        color: #fff;
        height: 50px;
        width: 100%;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
    }

.tiltup {
    display: block;
    transform: perspective(300px);
    animation: tiltup 0.7s;
    animation-fill-mode: forwards;
    padding-top: 0;
}

@keyframes tiltup {
    0% {
        bottom: -400px;
        opacity: 0;
        transform: rotateX(-80deg);
        border-top: solid 40px;
    }

    30% {
        opacity: 0;
    }

    100% {
        bottom: 0;
    }
}

.zoom {
    display: block;
    animation: zoom 0.7s;
    animation-fill-mode: forwards;
}

@keyframes zoom {
    0% {
        opacity: 0;
        transform: scale(0, 0);
    }

    30% {
        opacity: 0;
    }

    100% {
        bottom: 0;
    }
}

.hidden {
    display: none;
}

.reverse {
    animation-direction: reverse;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="input">
<input type="text" class="input">
<input type="text" class="input">
<input type="text" class="input">

<div class='cover hidden'></div>
<div class='modal hidden'>
    <div class='content'>
        <div class="numpad">
            <div id="container">
                <div>
                    <button class="shuffle keyBoardButton">1</button>
                    <button class="shuffle keyBoardButton">2</button>
                    <button class="shuffle keyBoardButton">3</button>
                </div>
                <div>
                    <button class="shuffle keyBoardButton">4</button>
                    <button class="shuffle keyBoardButton">5</button>
                    <button class="shuffle keyBoardButton">6</button>
                </div>
                <div>
                    <button class="shuffle keyBoardButton">7</button>
                    <button class="shuffle keyBoardButton">8</button>
                    <button class="shuffle keyBoardButton">9</button>
                </div>
                <div>
                    <button class="del keyBoardButton">X</button>
                    <button class="shuffle keyBoardButton">0</button>
                    <button class="faq keyBoardButton">?</button>
                </div>
            </div>
        </div>
    </div>

</div>

如您所见,当单击输入字段时,虚拟键盘将在模式窗口中打开,在添加数据之后,在其他输入文件处此数据将被复制。

因此,对于每个输入,我需要单独的数据。

1 个答案:

答案 0 :(得分:1)

看到这个摘要,您会有所了解。

$(document).ready(function () {

 var selected_inp;
    $(".numpad").hide();
    $('.input').click(function () {
       selected_inp = $(this);
        $('.numpad').fadeToggle('fast');
    });

    $('.del').click(function () {
        $('.input').val($('.input').val().substring(0, $('.input').val().length - 1));
    });
    $('.faq').click(function () {
        alert("It's virtual keyboard");
    })
    $('.shuffle').click(function () {
        //$('.input').val($('.input').val() + $(this).text());
        selected_inp.val($(this).text());
    });
    (function ($) {

        $.fn.shuffle = function () {

            var allElems = this.get(),
                getRandom = function (max) {
                    return Math.floor(Math.random() * max);
                },
                shuffled = $.map(allElems, function () {
                    var random = getRandom(allElems.length),
                        randEl = $(allElems[random]).clone(true)[0];
                    allElems.splice(random, 1);
                    return randEl;
                });

            this.each(function (i) {
                $(this).replaceWith($(shuffled[i]));
            });

            return $(shuffled);

        };

    })(jQuery);

});

$(".input").on("click", function () {
    $('.modal, .cover').removeClass("hidden");
    $('.modal').addClass("zoom");
});

//hide modal
$(".cover, .close").on("click", function () {
    $('.modal').attr('class', 'modal');
    $('.modal, .cover').addClass("hidden");
});
.keyBoardButton {
    margin: 13px;
}

.keyBoardButton {
    display: inline-block;
    border: 1px solid #fff;
    color: #fff;
    border-radius: 30px;
    -webkit-border-radius: 30px;
    -moz-border-radius: 30px;
    font-family: Verdana;
    width: auto;
    height: auto;
    font-size: 22px;
    padding: 20px 27px;
    background-color: #878787;
}

    .keyBoardButton:hover, .keyBoardButton:active {
        border: 1px solid #FFFFFF;
        color: #000;
        background-color: #fff;
    }

.cover {
    z-index: 1;
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: #333;
    top: 0;
    left: 0;
    opacity: .9;
}

.modal {
    z-index: 2;
    height: 390px;
    width: 350px;
    background-color: #262626;
    border-radius: 5px;
    text-align: center;
    border-top: solid 3px #262626;
    position: absolute;
    opacity: 0.9;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}

    .modal .content p {
        font-size: 2em;
        color: #fff;
        height: 50px;
        width: 100%;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
    }

.tiltup {
    display: block;
    transform: perspective(300px);
    animation: tiltup 0.7s;
    animation-fill-mode: forwards;
    padding-top: 0;
}

@keyframes tiltup {
    0% {
        bottom: -400px;
        opacity: 0;
        transform: rotateX(-80deg);
        border-top: solid 40px;
    }

    30% {
        opacity: 0;
    }

    100% {
        bottom: 0;
    }
}

.zoom {
    display: block;
    animation: zoom 0.7s;
    animation-fill-mode: forwards;
}

@keyframes zoom {
    0% {
        opacity: 0;
        transform: scale(0, 0);
    }

    30% {
        opacity: 0;
    }

    100% {
        bottom: 0;
    }
}

.hidden {
    display: none;
}

.reverse {
    animation-direction: reverse;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="input">
<input type="text" class="input">
<input type="text" class="input">
<input type="text" class="input">

<div class='cover hidden'></div>
<div class='modal hidden'>
    <div class='content'>
        <div class="numpad">
            <div id="container">
                <div>
                    <button class="shuffle keyBoardButton">1</button>
                    <button class="shuffle keyBoardButton">2</button>
                    <button class="shuffle keyBoardButton">3</button>
                </div>
                <div>
                    <button class="shuffle keyBoardButton">4</button>
                    <button class="shuffle keyBoardButton">5</button>
                    <button class="shuffle keyBoardButton">6</button>
                </div>
                <div>
                    <button class="shuffle keyBoardButton">7</button>
                    <button class="shuffle keyBoardButton">8</button>
                    <button class="shuffle keyBoardButton">9</button>
                </div>
                <div>
                    <button class="del keyBoardButton">X</button>
                    <button class="shuffle keyBoardButton">0</button>
                    <button class="faq keyBoardButton">?</button>
                </div>
            </div>
        </div>
    </div>

</div>