具體的需求: 在客戶資訊表格的操作列中,點選修改付款密碼按鈕,會跳到6位付款密碼輸入框元件頁面。同時,要求輸入框密文顯示、不可編輯、不可回退、即時顯示;到達6位數,自動進入確認支付密碼;確認支付密碼到達6位數,自動檢驗兩次輸入密碼的一致性,顯示確定按鈕。此功能是為了用於在銀行中,客戶用設備輸入密碼,櫃員不可見密碼,但櫃員可以進行提示操作。
具體的問題: 1、如何實現密文顯示,且每個框只能輸入1位數字;2、如何實現輸入框不可編輯、不可回退;3、如何檢驗兩次輸入密碼的一致性;4、如果自己的業務需要對鍵盤按鍵做限制,該怎麼處理。
一、程式碼總覽
實作6位付款密碼輸入框元件的程式碼如下,複製即可直接使用!
<template> <div > <!-- 密码输入框 --> <div class="input-box" > <!-- 输入密码 --> <div >{{ "输入密码" }}</div> <div class="input-content" @keyup="keyup" @input="inputEvent"> <input max="9" min="0" maxlength="1" data-index="0" v-model.number="state.input[0]" type="password" ref="firstinput" :disabled="state.disabledInput[0]" /> <input max="9" min="0" maxlength="1" data-index="1" v-model.number="state.input[1]" type="password" :disabled="state.disabledInput[1]" /> <input max="9" min="0" maxlength="1" data-index="2" v-model.number="state.input[2]" type="password" :disabled="state.disabledInput[2]" /> <input max="9" min="0" maxlength="1" data-index="3" v-model.number="state.input[3]" type="password" :disabled="state.disabledInput[3]" /> <input max="9" min="0" maxlength="1" data-index="4" v-model.number="state.input[4]" type="password" :disabled="state.disabledInput[4]" /> <input max="9" min="0" maxlength="1" data-index="5" v-model.number="state.input[5]" type="password" :disabled="state.disabledInput[5]" /> </div> <!-- 确认密码 --> <div >{{ "确认密码" }}</div> <div class="input-content" @keyup="confirmKeyUp" @input="confirmInputEvent"> <input max="9" min="0" maxlength="1" data-index="0" v-model.number="state.confirmInput[0]" type="password" ref="confirmfirstinput" :disabled="state.disabledConfirmInput[0]" /> <input max="9" min="0" maxlength="1" data-index="1" v-model.number="state.confirmInput[1]" type="password" :disabled="state.disabledConfirmInput[1]" /> <input max="9" min="0" maxlength="1" data-index="2" v-model.number="state.confirmInput[2]" type="password" :disabled="state.disabledConfirmInput[2]" /> <input max="9" min="0" maxlength="1" data-index="3" v-model.number="state.confirmInput[3]" type="password" :disabled="state.disabledConfirmInput[3]" /> <input max="9" min="0" maxlength="1" data-index="4" v-model.number="state.confirmInput[4]" type="password" :disabled="state.disabledConfirmInput[4]" /> <input max="9" min="0" maxlength="1" data-index="5" v-model.number="state.confirmInput[5]" type="password" :disabled="state.disabledConfirmInput[5]" /> </div> </div> <!-- 按钮 --> <div > <el-button type="info" :disabled="state.disabledConfirm" @click="reConfirm" :class="[state.disabledConfirm ? 'noActive' : 'active']">{{ "确定" }}</el-button> <el-button type="warning" @click="reset">{{ "重新输入" }}</el-button> </div> <!-- 提示区 --> <div > <p>{{ state.tipContent }}</p> </div> </div> </template> <script lang="ts" setup> import { nextTick, reactive, ref, onMounted } from "vue"; import { ElMessage, ElMessageBox } from 'element-plus' const state = reactive({ // 输入数组 input: ["", "", "", "", "", ""], // 确认输入数组 confirmInput: ["", "", "", "", "", ""], // 存放粘贴进来的数字 pasteResult: [], confirmPasteResult: [], // 一上来禁用确定按钮 disabledConfirm: true, // 输入框是否禁用 disabledInput: [false, false, false, false, false, false], disabledConfirmInput: [false, false, false, false, false, false], // 提示内容 tipContent: "请告知客户输入6位数字密码,输入完毕后,点击回车确认。" }) // 获取第一个元素的ref const firstinput = ref() const confirmfirstinput = ref() // 页面一加载就使第一个框聚焦 onMounted(() => { // 等待dom渲染完成,在执行focus,否则无法获取到焦点 nextTick(() => { firstinput.value.focus(); }); }) // @input的处理方法 // 解决一个输入框输入多个字符 const inputEvent = (e) => { var index = e.target.dataset.index * 1; var el = e.target; // 限制只能输入数字 el.value = el.value.replace(/[^\d]/g, ""); if (el.value.length >= 1) { // 密文显示、不可编辑、不可回退、即时显示 state.disabledInput[index] = true; if (el.nextElementSibling) { el.nextElementSibling.focus(); } } // 到达6位数,自动进入确认支付密码 if (!el.nextElementSibling) { confirmfirstinput.value.focus(); state.tipContent = "请告知客户再次输入6位数字密码,输入完毕后,点击回车确认。"; } } // @keydown的处理方法,根据业务需要添加 // 此示例没有使用 const keydown = (e) => { var index = e.target.dataset.index * 1; var el = e.target; // 回退键 if (e.key === 'Backspace') { if (state.input[index].length > 0) { state.input[index] = '' } else { if (el.previousElementSibling) { el.previousElementSibling.focus() state.input[index - 1] = '' } } } // 删除键 else if (e.key === 'Delete') { if (state.input[index].length > 0) { state.input[index] = '' } else { if (el.nextElementSibling) { state.input[1] = '' } } if (el.nextElementSibling) { el.nextElementSibling.focus() } } // 左键 else if (e.key === 'ArrowLeft') { if (el.previousElementSibling) { el.previousElementSibling.focus() } } // 右键 else if (e.key === 'ArrowRight') { if (el.nextElementSibling) { el.nextElementSibling.focus() } } // 上键 else if (e.key === 'ArrowUp') { if (Number(state.input[index]) * 1 < 9) { state.input[index] = (Number(state.input[index]) * 1 + 1).toString() } } // 下键 else if (e.key === 'ArrowDown') { if (Number(state.input[index]) * 1 > 0) { state.input[index] = (Number(state.input[index]) * 1 - 1).toString() } } } // @keyup的处理方法 const keyup = (e) => { var index = e.target.dataset.index * 1; // 如果为最后一个框,则输入框全部失焦 if (index === 5) { if (state.input.join("").length === 6) { document.activeElement.blur(); } } } // @input的处理方法 // 解决一个输入框输入多个字符 const confirmInputEvent = (e) => { var index = e.target.dataset.index * 1; var el = e.target; if (el.value.length >= 1) { // 密文显示、不可编辑、不可回退、即时显示 state.disabledConfirmInput[index] = true; if (el.nextElementSibling) { el.nextElementSibling.focus(); } } // 到达6位数,自动检验两次输入密码的一致性 if (!el.nextElementSibling) { // 一一比较元素值,有一个不相等就不等 for (let i = 0; i < state.input.length; i++) { if (state.input[i] !== state.confirmInput[i]) { state.tipContent = "请告知客户两次密码输入不一致,柜员点击重新输入,清空密码后请告知客户重新输入。"; return; } } state.tipContent = "密码合规,点击确定按钮进行修改。"; // 确定按钮变为可用 state.disabledConfirm = false; } } // @keydown的处理方法,根据业务需要添加 // 此示例没有使用 const confirmKeydown = (e) => { var index = e.target.dataset.index * 1; var el = e.target; // 回退键 if (e.key === 'Backspace') { if (state.confirmInput[index].length > 0) { state.confirmInput[index] = '' } else { if (el.previousElementSibling) { el.previousElementSibling.focus() state.confirmInput[index - 1] = '' } } } // 删除键 else if (e.key === 'Delete') { if (state.confirmInput[index].length > 0) { state.confirmInput[index] = '' } else { if (el.nextElementSibling) { state.confirmInput[1] = '' } } if (el.nextElementSibling) { el.nextElementSibling.focus() } } // 左键 else if (e.key === 'ArrowLeft') { if (el.previousElementSibling) { el.previousElementSibling.focus() } } // 右键 else if (e.key === 'ArrowRight') { if (el.nextElementSibling) { el.nextElementSibling.focus() } } // 上键 else if (e.key === 'ArrowUp') { if (Number(state.confirmInput[index]) * 1 < 9) { state.confirmInput[index] = (Number(state.confirmInput[index]) * 1 + 1).toString() } } // 下键 else if (e.key === 'ArrowDown') { if (Number(state.confirmInput[index]) * 1 > 0) { state.confirmInput[index] = (Number(state.confirmInput[index]) * 1 - 1).toString() } } } // @keyup的处理方法 const confirmKeyUp = (e) => { var index = e.target.dataset.index * 1; // 如果为最后一个框,则输入框全部失焦 if (index === 5) { if (state.confirmInput.join("").length === 6) { document.activeElement.blur(); } } } // 重新输入 const reset = () => { state.disabledConfirm = true; state.tipContent = "请告知客户输入6位数字密码,输入完毕后,点击回车确认。"; state.input = ["", "", "", "", "", ""]; state.confirmInput = ["", "", "", "", "", ""]; state.disabledInput = [false, false, false, false, false, false]; state.disabledConfirmInput = [false, false, false, false, false, false]; // 等待dom渲染完成,在执行focus,否则无法获取到焦点 nextTick(() => { firstinput.value.focus(); }); } // 确认修改 const reConfirm = () => { ElMessageBox.confirm( '是否确定修改?', '温馨提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', } ) .then(() => { // 此处调修改支付密码接口 ElMessage({ type: 'success', message: '修改成功!', }) }) .catch(() => { ElMessage({ type: 'info', message: '已取消修改!', }) }) } </script> <style lang="scss" scoped> .input-box { .input-content { width: 512px; height: 60px; display: flex; align-items: center; justify-content: space-between; input { color: inherit; font-family: inherit; border: 0; outline: 0; border-bottom: 1px solid #919191; height: 60px; width: 60px; font-size: 44px; text-align: center; } } input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { appearance: none; margin: 0; } } .noActive { color: #fff !important; border-width: 0px !important; background-color: #ccc !important; } .active { color: #fff !important; border-width: 0px !important; background-color: #67c23a !important; } </style>
二、問題解析
1、問:如何實作密文顯示,且每個框只能輸入1位數字?
如果想要進行密文輸入,只需要將輸入框的類型設為"password"即可。對於實現每個框只能輸入1位數字,這裡只使用輸入框的maxlength屬性效果並不完美,可能會出現限制不住的情況,需要在@input事件中,判斷當前元素值的長度,如果大於等於1,則透過nextElementSibling.focus(),讓遊標聚焦到下一個兄弟元素上去。
2、問:如何實現輸入框不可編輯、不可回退?
答案:使用了輸入框的disabled屬性,透過在@input事件中,將目前輸入元素的disabled屬性變成true即可。為了方便後續的取得和修改,我們將輸入框的disabled屬性值分別儲存在一個陣列中。
3、問:如何檢驗兩次輸入密碼的一致性?
答案:使用了最簡單的for循環,遍歷輸入密碼數組和確認密碼數組,一一比較它們的元素值,有一個不相等就不等,透過return;結束整個函數的執行。
4、問:如果自己的業務需要對鍵盤按鍵做限制,該怎麼處理?
答案:可以為輸入框新增@keydown或@keyup事件,在回呼內部透過對key做判斷,來對不同的按鍵做一些業務的處理。
以上是vue3怎麼實現6位支付密碼輸入框的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Netflix使用React作為其前端框架。 1)React的組件化開發模式和強大生態系統是Netflix選擇它的主要原因。 2)通過組件化,Netflix將復雜界面拆分成可管理的小塊,如視頻播放器、推薦列表和用戶評論。 3)React的虛擬DOM和組件生命週期優化了渲染效率和用戶交互管理。

Netflix在前端技術上的選擇主要集中在性能優化、可擴展性和用戶體驗三個方面。 1.性能優化:Netflix選擇React作為主要框架,並開發了SpeedCurve和Boomerang等工具來監控和優化用戶體驗。 2.可擴展性:他們採用微前端架構,將應用拆分為獨立模塊,提高開發效率和系統擴展性。 3.用戶體驗:Netflix使用Material-UI組件庫,通過A/B測試和用戶反饋不斷優化界面,確保一致性和美觀性。

NetflixusesAcustomFrameworkcalled“ Gibbon” BuiltonReact,notReactorVuedIrectly.1)TeamSperience:selectBasedonFamiliarity.2)ProjectComplexity:vueforsimplerprojects:reactforforforproproject,reactforforforcompleplexones.3)cocatizationneedneeds:reactoffipicatizationneedneedneedneedneedneeds:reactoffersizationneedneedneedneedneeds:reactoffersizatization needefersmoreflexibleise.4)

Netflix在框架選擇上主要考慮性能、可擴展性、開發效率、生態系統、技術債務和維護成本。 1.性能與可擴展性:選擇Java和SpringBoot以高效處理海量數據和高並發請求。 2.開發效率與生態系統:使用React提升前端開發效率,利用其豐富的生態系統。 3.技術債務與維護成本:選擇Node.js構建微服務,降低維護成本和技術債務。

Netflix主要使用React作為前端框架,輔以Vue用於特定功能。 1)React的組件化和虛擬DOM提升了Netflix應用的性能和開發效率。 2)Vue在Netflix的內部工具和小型項目中應用,其靈活性和易用性是關鍵。

Vue.js是一種漸進式JavaScript框架,適用於構建複雜的用戶界面。 1)其核心概念包括響應式數據、組件化和虛擬DOM。 2)實際應用中,可以通過構建Todo應用和集成VueRouter來展示其功能。 3)調試時,建議使用VueDevtools和console.log。 4)性能優化可通過v-if/v-show、列表渲染優化和異步加載組件等實現。

Vue.js適合小型到中型項目,而React更適用於大型、複雜應用。 1.Vue.js的響應式系統通過依賴追踪自動更新DOM,易於管理數據變化。 2.React採用單向數據流,數據從父組件流向子組件,提供明確的數據流向和易於調試的結構。

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具