如图。我们一般做商城类的项目不免会用到支付密码输入框,我研究了下并决定发上来,也当作是自己成长路上的一点小小的记录。本次介绍的是基于vue的项目

html:

<template>
<div class='am_payPwd' :id="`ids_${id}`">
<input type="password"
maxlength="1"
@input="changeInput"
@click="changePwd"
v-model="pwdList[i]"
@keyup="keyUp($event)"
@keydown="oldPwdList = pwdList.length"
class="shortInput"
v-for="(v, i) in 6" :key="i">
</div>
</template>

css:

.am_payPwd {
display: inline-block;
width: 242px;
height: 40px;
border: 1px solid #999;
border-radius: 5px;
padding: 10px 0;
position: relative;
margin-left: 1px;
.shortInput {
text-align: center;
font-size: 20px;
float: left;
width: 40px;
height: 20px;
color: #333;
outline: #ff0067;
&:not(:last-child) {
border-right: 1px solid #999;
}
}
}

js:

  data () {
return {
pwdList: [],
oldPwdList: [],
isDelete: false,
ipt: ''
}
},
props: {
id: String, // 当一个页面有多个密码输入框时,用id来区分
default: '1'
},
mounted () {  
this.ipt = document.querySelectorAll(`#ids_${this.id} .shortInput`)
},
methods: {
keyUp (ev) {
let index = this.pwdList.length
if (!index) return
if (ev.keyCode === 8) {
this.isDelete = true
if (this.oldPwdList === this.pwdList.length) {
if (index === this.pwdList.length) {
this.pwdList.pop()
}
index--
} else {
index > 0 && index--
}
this.ipt[index].focus()
} else if (this.isDelete && index === this.pwdList.length && /^\d$/.test(ev.key)) {
this.isDelete = false
this.pwdList.pop()
this.pwdList.push(ev.key)
this.ipt[this.pwdList.length] && this.ipt[this.pwdList.length].focus()
}
this.$emit('getPwd', this.pwdList.join(''))
},
changePwd () {
let index = this.pwdList.length
index === 6 && index--
this.ipt[index].focus()
},
changeInput () {
let index = this.pwdList.length
let val = this.pwdList[index - 1]
   if (!/[0-9]/.test(val)) { 
this.pwdList.pop()          
return       
}
if (!val) {
this.pwdList.pop()
index--
if (index > 0) this.ipt[index - 1].focus()
} else {
if (index <) this.ipt[index].focus()
}
}
}

如有考虑不周的,请指出

分享一个PC端六格密码输入框写法的更多相关文章

  1. 怎样写一个PC端使用的操盘手软件(用来买卖股票,查看报表,行情)

    我们想写一个操盘手软件,对于操盘而言,首先是快,然后是资料尽可能丰富,最好能看到行情,报表什么的.只是windows上写软件看似基础,实际上都不怎么好弄,用C++开发确实可以实现所有功能,估计光研发费 ...

  2. Supalle-Admin-Layout,一个PC端和手机端都合适用的后台页面模板

    Supalle-Admin-Layout主要使用有Vue.Element-UI.layui-icon,Ajax实现采用Fetch(是有这个打算,不过目前是jQuery.). 源码地址:https:// ...

  3. 分享一个移动端rem布局的适配mixin

    /*================================================================ 以下为基于ip5 宽度320做的适配,标准html{font-si ...

  4. PC端页面适应不同的分辨率的方法 (转载)

    原文地址:https://blog.csdn.net/fengzhen8023/article/details/81281117 上周完成一个PC端的项目,对于我这样的小白来说,这个项目里面最大的问题 ...

  5. JavaScript判断移动端及pc端访问不同的网站

    JavaScript判断移动端及pc端访问不同的网站 现在很多网站都是分为两个版本,一个pc端的一个移动端的(响应式除外),针对这两个版本,就需要对访问的设备进行判断,如果是pc,就直接访问pc网站, ...

  6. 自动识别移动端还是PC端

    平时在开发中经常会遇到这样的需求,除了开发PC端之外,还会同时开发移动端.对于简单的页面,可以使用bootstrap之类的框架实现响应式页面,可是当页面很复杂的时候,就需要开发一个移动端页面,一个PC ...

  7. CoCos2dx开发:PC端调试运行正常但打包apk文件后在手机上点击闪退

    记:今天调试时出现的一个PC端调试运行正常,但打包apk文件后在手机上点击闪退的问题. 可能在不同的情况条件下,会有不同的原因导致apk安装后闪退问题.拿android studio等软件来说,开发安 ...

  8. 自适应PC端网页制作使用REM

    做一个PC端的网页,设计图是1920X1080的. 要在常见屏上显示正常(比例正确可) 1280X720 1366X768 1440X900 1920X1080 使用了几种办法 1.内容在一屏内显示的 ...

  9. Unity输出PC端(Windows) 拖拽文件到app中

    需求:给策划们写一个PC端(Window)的Excel导表工具.本来用OpenFile打开FileExplorerDialog后让他们自己选择想要添加的Excel文件就行了,结果有个需求是希望能拖拽E ...

随机推荐

  1. windows常用命令集锦

    开始→运行→输入的命令集锦 gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址侦测器 explorer-------打开资源管理器 lo ...

  2. App后台开发架构实践笔记

    1 App后台入门 1.1 App后台的功能 (1)远程存储数据: (2)消息中转. 1.2 App后台架构 架构设计的流程 (1) 根据App的设计,梳理出App的业务流程: (2) 把每个业务流程 ...

  3. JDBC操作数据库的基本步骤:

    JDBC操作数据库的基本步骤: 1)加载(注册)数据库驱动(到JVM). 2)建立(获取)数据库连接. 3)创建(获取)数据库操作对象. 4)定义操作的SQL语句. 5)执行数据库操作. 6)获取并操 ...

  4. Sharepoint学习笔记—修改SharePoint的Timeouts (Execution Timeout)

    有时在Sharepoin中有些执行任务可能会超过Sharepoint环境默认的Timout限制,这种情况下系统会报"Request Timed out"错误.对此我们可以在两个层次 ...

  5. 【NLP_Stanford课堂】句子切分

    依照什么切分句子——标点符号 无歧义的:!?等 存在歧义的:. 英文中的.不止表示句号,也可能出现在句子中间,比如缩写Dr. 或者数字里的小数点4.3 解决方法:建立一个二元分类器: 检查“.” 判断 ...

  6. 【Leetcode】【Easy】Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  7. 数字(int)转字符串和字符串转数字(int)

    室友去面试,问了一个字符串转成数字的算法题,室友没搞出来,我心想,这个不是很简单的吗?于是动手在纸上画了画代码.画完后,总感觉哪里不对,最后一个个挖掘,才发现,尼玛,这到处都是坑啊---特此记录一下中 ...

  8. IOS 集成友盟分享

    #import <Foundation/Foundation.h> @interface UMSocialSinaHandler : NSObject +(void)openSSOWith ...

  9. nbu集群Alwayson相关问题

    Alwayson 1. Alwayson 是否依赖于域环境? 答: 是, alwayson依赖于故障转移群集(只有在故障转移群集中的SQL Server 才能启动高可行性组功能),而故障转移群集愈依赖 ...

  10. nginx 图片,js,css等文件允许跨域

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ { #允许跨域请求 add_header Access-Control-Allow-Ori ...