The implementation of HTML5 mobile price input keyboard

Time:2020-2-10

Simple implementation of mobile terminal input price keyboard

HTML:

<div class="main">
    <div id="show-price">

    </div>
    <div class="keyboard">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
        <div>9</div>
        <div>.</div>
        <div>0</div>
        < div > delete < / div >
    </div>
</div>

CSS:


.keyboard {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 240px;
    display: flex;
    flex-wrap: wrap;
}

.keyboard div {
    width: 30%;
    height: 50px;
    margin: 5px;
    text-align: center;
    line-height: 50px;
    border-radius: 5px;
    background: #eee;
}

JS:

<script src="../js/jquery-3.4.1.min.js"></script>
<script>
    window.onload = function () {
        let key = $('.keyboard div');
        let keyStr = ''
        key.click(function () {
            let str = ''
            Let elestr = $(this). Html() = 'Delete'? ': $(this). Html(); // do you want to delete
            If (Keystr. Length < = 0 & & elestr = = '0') return; // the first place cannot be 0
            Keystr = Keystr + elestr; // splicing the clicked numbers
            If (elestr = = '') Keystr = keystr.substr (0, Keystr. Length - 1); // delete
            For (let I = 0; I < Keystr. Length; I + +) {// traversal
                If (Keystr [i] = = '.') {// judge whether it is
                    If (str.indexof ('.') = = - 1) STR = STR + Keystr [i]; // Yes, and it does not exist
                }Else STR = STR + Keystr [i]; // no, just splice
            }
            Show price ($('(show price'), STR); // rendering
        })

        function showPrice(ele, str) {
            let htmlStr = ''
            for (let i = 0; i < str.length; i++) {
                htmlStr = htmlStr + `<span>${str[i]}</span>`
            }
            ele.html(htmlStr);
        }
    </script>

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.