实现原理:

默认input第一个带光标,第一个输完自动跳转到第一个输入框,以此类推,

当删除某一个输入框中的值重新输入,输入完后自动跳转到下一个

代码实现:

<div class="container">
<h2>Verify Your Account</h2>
<p>We emailed you the six digit code to cool_guy@email.com<br />Enter the code below to confirm your email address.</p>
<div class="code-container">
<input type="number" class="code" placeholder="0" min="0" max="9" required>
<input type="number" class="code" placeholder="0" min="0" max="9" required>
<input type="number" class="code" placeholder="0" min="0" max="9" required>
<input type="number" class="code" placeholder="0" min="0" max="9" required>
<input type="number" class="code" placeholder="0" min="0" max="9" required>
<input type="number" class="code" placeholder="0" min="0" max="9" required>
</div>
<small class="info">THis is design only. We didn't actually send you an email as we don't have your email, right?</small>
</div>
let codes = document.querySelectorAll('.code')
codes[0].focus()
codes.forEach((code,idx) => {
code.addEventListener('keydown', (e) =>{
if(e.key >= 0 && e.key <=9) {
codes[idx].value = ''
setTimeout(() => codes[idx + 1].focus(), 10)
}else if (e.key === 'Backspace') {
setTimeout(() => codes[idx - 1].focus(), 10)
}
})
})
@import url('https://fonts.googleapis.com/css?family=Muli:300,700&display=swap');
*{
box-sizing: border-box;
}
body{
background: #fbfcfe;
font-family: "Muli", sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}
.container{
padding: 30px;
background: #fff;
border: 3px solid #000;
border-radius: 10px;
max-width: 1000px;
text-align: center;
}
.code-container{
display: flex;
align-items: center;
justify-content: center;
margin: 40px 0;
}
.code{
border-radius: 5px;
font-size: 75px;
height: 120px;
width: 100px;
border: 1px solid #eee;
margin: 1%;
text-align: center;
font-weight: 300;
-moz-appearance: textfield;
}
.code::-webkit-outer-spin-button,
.code::-webkit-inner-spin-button{
-webkit-appearance: none;
margin: 0;
}
.code:valid{
border: #3498bd;
box-shadow: 0 10px 10px -5px rgba(0, 0, 0, 0.25);
}
.info{
background: #eaeaea;
display: inline-block;
padding: 10px;
line-height: 20px;
max-width: 400px;
color: #777;
border-radius: 5px;
}
@media(max-width:600px){
.code-container{
flex-wrap: wrap;
}
.code{
font-size: 60px;
height: 80px;
max-width: 70px;
}
}

效果实现:

Js实现监听input输入的更多相关文章

  1. js 实时监听input中值变化

    注意:用到了jquery需要引入jquery.min.js. 需求: 1.每个地方需要分别打分,总分为100; 2.第一个打分总分为40; 3.第二个打分总分为60. 注意:需要判断null.&quo ...

  2. 实时监听input输入内容的N种方法

    现在有一个需求,需要我们实时监听input输入框中的内容,从而带来更好的用户体验,而不是等我们全部输入完毕才告诉我们格式不对首先我们创建一个input输入框 <form name='loginF ...

  3. 微信小程序监听input输入并取值

    小程序的事件分为两种,冒泡和非冒泡事件,像<form/>的submit事件,<input/>的input事件,<scroll-view/>的scroll事件等非冒泡 ...

  4. 实时监听input输入的变化(兼容主流浏览器)【转】

    遇到如此需求,首先想到的是change事件,但用过change的都知道只有在input失去焦点时才会触发,并不能满足实时监测的需求,比如监测用户输入字符数. 在经过查阅一番资料后,欣慰的发现firef ...

  5. [转] 实时监听input输入的变化(兼容主流浏览器)

    遇到如此需求,首先想到的是change事件,但用过change的都知道只有在input失去焦点时才会触发,并不能满足实时监测的需求,比如监测用户输入字符数. 在经过查阅一番资料后,欣慰的发现firef ...

  6. 实时监听input输入的变化(兼容主流浏览器)

    遇到如此需求,首先想到的是change事件,但用过change的都知道只有在input失去焦点时才会触发,并不能满足实时监测的需求,比如监测用户输入字符数. 在经过查阅一番资料后,欣慰的发现firef ...

  7. js监听input输入字符变化

    <p class="text-input"> <input type="text" id="username" autoC ...

  8. js实时监听input中值得变化

    <!DOCTYPE html> <html> <head> <title>zepto</title> <meta name=" ...

  9. js实时监听input中值的变化

    $(function(){ $('#inputid').bind('input propertychange', function() { // input 中的值 var params = $(th ...

  10. 实时监听input输入情况

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

随机推荐

  1. 函数:3ds max 给选择对象设置轴心点

    ------轴心点函数大全------函数名称中的字母含义:------w:西 e:东 n:北 s:南 b:底 c:中心 t:顶 m:间 如:wnb表示西北下 smt表示南中上 fn pivot_wn ...

  2. Oracle UNDO表空间的管理

    Oracle UNDO表空间的管理 UNDO表空间的管理是Oracle DBA最重要的日常工作之一,UNDO表空间用来暂时存储DML操作的数据,其主要作用有: 1.事务回滚 2.实例恢复 3.读一致性 ...

  3. CentOS7安裝 Nginx + php7 + php-fpm

    原文 : https://ivanagyro.medium.com/於centos7安裝-nginx-php7-php-fpm-laravel5-6-df8631681acf安装nginx 1/ 用y ...

  4. elementui树状结构添加右键点击事件

    <el-tree :highlight-current="highlight" :data="folderList" :props="defau ...

  5. fastdfs java客户端操作

    https://github.com/happyfish100/fastdfs-client-java 到此处下载下来demo 这里采用maven的方式 mvn clean install 上传到本地 ...

  6. maven-阿里云镜像

    <mirror>     <id>nexus-aliyun</id>     <mirrorOf>*,!jeecg,!jeecg-snapshots&l ...

  7. Request processing failed;

    用 ssm 框架修改数据库数据时,出现了 Request processing failed; nested exception is org.mybatis.spring.MyBatisSystem ...

  8. (0724) 格雷码 verilog

    https://blog.csdn.net/gordon_77/article/details/79489548 assign gray_value = (binary_value >> ...

  9. 【java数据结构与算法】插入排序

    [插入排序解析]起始:假设第一个元素为已经排好序那么我们就要从数组的第二个元素开始每一轮确定1一个元素的正确位置所以外层循环的控制变量为 [1,arr.length)的左闭右开区间外层循环控制比较轮次 ...

  10. TCP通信实现两个主机之间的信息交互

    TCP通信概述TCP协议用来控制两个网络设备之间的点对点通信,两端设备按作用分为客户端和服务端.服务端为客户端提供服务,通常等待客户端的请求信息,有客户端请求到达之后,及时提供服务和返回响应消息:客户 ...