Vue项目手机验证码输入框制作

博客围绕手机验证码输入框展开,但具体内容未给出。手机验证码输入框在信息技术领域常用于用户身份验证等场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
    <div>
        <div class="number-input" id="my-vcode">
            <input type="tel" maxlength="1" v-model="fourNumerObj.input1" id="input1" @input="getNextFocus(1)"/>
            <input type="tel" maxlength="1" v-model="fourNumerObj.input2" id="input2" @input="getNextFocus(2)"/>
            <input type="tel" maxlength="1" v-model="fourNumerObj.input3" id="input3" @input="getNextFocus(3)"/>
            <input type="tel" maxlength="1" v-model="fourNumerObj.input4" id="input4" @input="getNextFocus(4)"/>
            <input type="tel" maxlength="1" v-model="fourNumerObj.input5" id="input5" @input="getNextFocus(5)"/>
            <input type="tel" maxlength="1" v-model="fourNumerObj.input6" id="input6"/>
        </div>
    </div>
</template>

<script>

    export default {
        name: 'login',
        data() {
            return {
                fourNumerObj: {
                    input1: '',
                    input2: '',
                    input3: '',
                    input4: '',
                    input5: '',
                    input6: ''
                }
            }
        },
        components: {},
        mounted: function () {
            //  手机删除按钮监听事件,验证码连续删除
            let vcodeInput = document.getElementById('my-vcode');
            let inputArr = vcodeInput.getElementsByTagName('input');
            for (let i = 0; i < inputArr.length; i++) {
                inputArr[i].addEventListener('keydown', function (event) {
                    var evt = event || window.event;
                    if (inputArr[i].value === '' && evt.keyCode === 8) {
                        // 如果为空且按下退回键
                        if (i !== 0) {
                            inputArr[i - 1].focus();
                        }
                    }
                })
            }
        },
        methods: {
            getNextFocus: function (param) {
                let curInputObj = document.getElementById('input' + param);
                let nextInputObj = document.getElementById('input' + (param + 1));
                // 如果当前input的值不为空,且下一个input的值为空,下一个input设置聚焦
                if (param && (curInputObj.value || curInputObj.value === 0) && !nextInputObj.value && nextInputObj.value !== 0) {
                    nextInputObj.focus();
                }
            }
        }
    }
</script>

<style scoped>
    .number-input {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
        margin: 100px auto;
    }

    .number-input input {
        border: none;
        border-bottom: 1px solid #dcdcdc;
        margin: 0 10px;
        width: 30px;
        font-size: 18px;
        height: 44px;
        line-height: 44px;
        text-align: center;
    }
</style>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值