Input的常用业务实例实现

news/2024/10/18 16:55:42/

最近写业务的时候一直在和Input打交道,下面几种常见的业务代码分享给大家,以下代码均使用vue框架语法。

验证码四个Input的焦点获取、连删实现,复制

使用场景:验证码的校验,在输入一个验证码之后,焦点移动到自动向像一个输入框

焦点获取

  • 实现思路: 获取下一个dom节点,然后focus
<Inputv-for="(code, ind) in codeNameMap":id="`input${code}`" // 用户获取input节点:key="code"v-model="exchangeCode[code]"class="code-input":max-length="1"@input="(e) => ind < 4 && handleInput(e, `input${codeNameMap[ind + 1]}`)" // 输入的时候获取下一个位置焦点
/>       
const exchangeCode: any = reactive({one: '',two: '',thr: '',four: '',})
const domFocus = (code: string) => {nextTick(() => {const dom = document.getElementById(code)const inputDom = dom?.getElementsByTagName('input')if (inputDom) inputDom[0].focus() // 获取焦点、 // 使用的组件所以input内容较深,大家可根据自己使用情况去获取})}function handleInput(event: any, nextInput: string) {if (event.target.value.length === event.target.maxLength && nextInput !== null) {domFocus(nextInput)}}  

复制功能

  • 实现思路: 获取当前输入框的复制内容,再主动赋值,这里赋值的时候提高用户体验,也将焦点获取定位
// input 节点内加入该方法,参照上面Input、这里ind表示需要从哪一个input框开始复制@paste="(e:any) => handlePaste(e, ind)"
  const handlePaste = (e: any, ind: number) => {const codeNameMap = Object.keys(exchangeCode)const copyText = e.clipboardData.getData('text') // 获取复制内容const needCodeLen = codeNameMap.lengthconst copyTextLen = copyText.lengthconst copyLen = copyTextLen < needCodeLen ? copyTextLen + ind : needCodeLen // 通过复制内容长度和需要的code长度 获取 真正需要复制的内容及位置for (let i = ind; i < copyLen; i++) {exchangeCode[codeNameMap[i]] = copyText[i - ind]}// 焦点移动if (needCodeLen - ind > copyTextLen) {const focusDom = `input${codeNameMap[ind + copyTextLen]}`domFocus(focusDom)e.preventDefault() // 禁止input操作,否则焦点会变成双跳}}

连删

实现思路: 监听键盘时间,在键盘为删除的时候清空input数据,并且焦点回到上一个Input

// input 节点内加入该方法,参照上面Input、这里ind表示需要从哪一个input框开始删除
@keyup="(e:any)=>handleKeyUp(e,ind)"
// 在Input内加入方法 @keyup="(e:any)=>handleKeyUp(e,ind)"const handleKeyUp = (event: any, ind: number) => {if (event.key === 'Backspace' || event.key === 'Delete') {if (ind > 0) {const focusDom = `input${codeNameMap[ind - 1]}`domFocus(focusDom)}}}

输入框特殊字符实时校验, 及长度限制

  • 使用场景:实时校验昵称之类的输入限制,
  • 注意:实时校验会遇到一个问题,无法连续输入长中文
  • 实现思路: 使用input的API,compositionStart compositionEnd 在输入长中文的时候,不对input做实时校验,长中文输入完成再进行校验
<Inputv-model="nick_name"placeholder="请输入昵称"clearable@input="handleInputNickName"@compositionstart="compositionStart"@compositionend="compositionEnd"
/>   
  const DEFAULT_LEN = 11const isInputChinese = ref(false) // 是否在输入长中文const handleInputNickName = e => {if (isInputChinese.value) {return}const value = e.target.valueconst pattern = /[^\u4e00-\u9fa5a-zA-Z\d]/g // 只保留中英文数字const patternTest = /^[a-zA-Z0-9\u4e00-\u9fa5]+$/const isValid = patternTest.test(value)if (!isValid && value.trim()) {toast.warning('不支持特殊字符')}const newValue = value.replace(pattern, '')if (e.target.value.length > DEFAULT_LEN) {toast.warning('最多支持11个字符')}e.target.value = newValue.slice(0, 11) // 字数截断}const compositionStart = () => {isInputChinese.value = true}const compositionEnd = e => {// 长中文输入结束,需要对字符数量进行判断并且截断isInputChinese.value = falseconst newValue = e.target.valueif (newValue.length > DEFAULT_LEN) {toast.warning('最多支持11个字符')e.target.value = newValue.slice(0, 11)}}

http://www.ppmy.cn/news/901132.html

相关文章

ES系列--分析器

一、前言 ES进行文档分析就会涉及到分析器&#xff0c;无论是内置的分析器&#xff0c;还是自定义的分析器&#xff0c;都是由一个分词器&#xff08;tokenizers&#xff09; 、0或多个词项过滤器&#xff08;token filters&#xff09;、0或多个字符过滤器&#xff08;charact…

努力,让衣食更出色,让面试更自信!

是什么&#xff0c;让#衣食更出色 &#xff1f; 是面试前的精心准备 是拿到offer后的喜悦庆祝 还是… 11月1日锁定小天鹅冰箱洗衣机京东直播 更多精彩邀你一同揭晓&#xff01;

乘风破浪初心不改,实力出众当红不让

#小天鹅冰箱高湿冻鲜系列 #冷冻室90%高湿恒温环境 肉类保鲜不泛白

绝美匠心铸品质,集万茜宠爱于一身

天工造物华丽耀世 集万茜宠爱于一身 #小天鹅冰箱高湿冻鲜系列 #穹硬弧面设计 以匠心 铸大美

左耳进,右耳出?

导读 同时保有两种截然相反的观念还能正常行事&#xff0c;这是第一流智慧的标志 -- 菲茨杰拉德 周末愉快。 1. 术后&#xff0c;我左耳听力为零。 俗话说&#xff1a;左耳进、右耳出&#xff0c;这敢情太好&#xff0c;啥也不用听进去了。 但实际上&#xff0c;研究表明大脑对…

python对dataframe索引的操作

目录 假如有个dataframe如下&#xff0c;这里需要去除第一个aa&#xff0c;保留最后一个aa 关键代码 # 如果你想保留第一个aa&#xff0c;那么keep就是first df.reset_index().drop_duplicates(subsetindex, keepfirst).set_index(index)结果如下&#xff1a; 原文链接&am…

WinRAR_v6.01压缩文件包必备软件

介绍&#xff1a; WinRAR压缩文件管理器&#xff0c;知名解压缩软件&#xff0c;电脑装机必备软件&#xff0c;国内最流行最好用的压缩文件管理器、解压缩必备软件。 它提供RAR和ZIP文件的完整支持&#xff0c;能解压ARJ、CAB、LZH、ACE、TAR、GZ、UUE、BZ2、JAR、ISO等多种格…

【实用工具】文件压缩

引入依赖 <dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.8</version></dependency>使用 将某个目录下面的所有文件&#xff08;包含子目录&#xff09;压缩到某个文件中 //将a…