vue3实现 多个input框输入 自动聚焦下一个

我最近要实现 车牌号输入框的功能,vant里有密码输入框,功能一样,但是vant只有数字键盘。不符合我的需求,所

<!-- 查询客户 -->
<template>
    <div class="page" v-show="data.show">
        <div class="licensePlate">
            <div class="input">
                <input type="text" class="inputValue" v-for="(item, index) in data.inputList" maxlength="1"
                    v-model="item.pinless" :key="index" @keydown="backClick" ref="inputList">
            </div>
        </div>

    </div>
</template>

<script>
import { looseIndexOf } from '@vue/shared'
import { onMounted, reactive, getCurrentInstance, watch, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
export default {
    setup() {

        let routr = useRouter()
        let route = useRoute()

        let data = reactive({
            // 输入框循环的数组
            inputList: [
                { pinless: "" },
                { pinless: "" },
                { pinless: "" },
                { pinless: "" },
                { pinless: "" },
                { pinless: "" },
                { pinless: "" },
                { pinless: "" },
            ],
        })

        let inputList = reactive({

        })

        // 获取inputDom元素
        let inputValue = getCurrentInstance()
        onMounted(() => {
            inputList = inputValue.refs.inputList
        })
        // 监视input框输入,自动聚焦下一个
        watch(data.inputList, (newValue) => {
            for (let inx = 0; inx < newValue.length; inx++) {
                if (newValue[inx].pinless != '') {
                    if (inx === 7) {
                        for (let index = 0; index < newValue.length; index++) {  // 用户从中间开始输入情况处理
                            if (newValue[index].pinless === '') {
                                inputList[index].focus()
                                return
                            }
                        }
                    } else {
                        inputList[inx + 1].focus()
                    }
                }
            }
        })
        // input框删除事件
        const backClick = ((val) => {
            if (val.keyCode === 8) {
                setTimeout(() => {
                    for (let index = 0; index < data.inputList.length; index++) {
                        if (data.inputList[index].pinless === '') {
                            if (index === 0) {
                                for (let i = 0; i < data.inputList.length; i++) { // 用户从中间删除情况处理
                                    if (data.inputList[i].pinless != '') {
                                        inputList[i].focus()
                                    }
                                }
                                return
                            } else {
                                inputList[index - 1].focus() // 删除上一个input聚焦
                                return
                            }
                        }
                    }
                })
            }
        })


        return {
            data,
            backClick,
        }
    }
}
</script>

<style scoped lang="scss">
.page {
    width: 6.86rem;
    margin: 0 auto;

    .licensePlate {
        
        .input {
            margin-top: 0.24rem;
            display: flex;
            justify-content: space-between;

            .inputValue {
                outline: none;
                padding: 0;
                width: 0.5rem;
                height: 1rem;
                border: 0.016rem solid #CCCCCC;
                border-radius: 0.16rem;
                align-items: center;
                justify-content: space-evenly;
                padding-left: 0.3rem;
            }

          
        }
    }

   
}

</style>

以手搓了一个。其中考虑到,中间输入、中间删除的情况

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值