JS/JQ实现小程序/H5验证码页面

话不多说,先上效果图

核心代码

1、html/css关键代码

html 复制代码
<div class="obtain-verification-code">
    <div class="obtain-verification-code-input-content">
        <input id="input-0" class="verification-code-input" max="9" min="0" maxlength="1" data-index="0" type="number" />
        <input id="input-1" class="verification-code-input" max="9" min="0" maxlength="1" data-index="1" type="number" />
        <input id="input-2" class="verification-code-input" max="9" min="0" maxlength="1" data-index="2" type="number" />
        <input id="input-3" class="verification-code-input" max="9" min="0" maxlength="1" data-index="3" type="number" />
        <input id="input-4" class="verification-code-input" max="9" min="0" maxlength="1" data-index="4" type="number" />
        <input id="input-5" class="verification-code-input" max="9" min="0" maxlength="1" data-index="5" type="number" />
    </div>
</div>

<style lang="less">
.obtain-verification-code {
    margin-top: 20px;
    .obtain-verification-code-input-content {
        box-sizing: border-box;
	    padding-left: 10px;
		padding-right: 10px;
		width: 100%;
		height: 40px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		input {
			background: #f8f8f8;
			color: #111111;
			font-family: inherit;
			font-weight: bold;
			border: 0;
			outline: 0;
			height: 40px;
			width: 40px;
			font-size: 20px;
			text-align: center;
			border-radius: 4px;
		}
    }
    input::-webkit-outer-spin-button,
	input::-webkit-inner-spin-button {
		appearance: none;
		margin: 0;
	}
}
</style>

2、js关键代码

javascript 复制代码
autoSubmodify(code) {
	let strCode = ''
	if(!code) {
		let val = $("#input-0").val() || ''
		let val1 = $("#input-1").val() || ''
		let val2 = $("#input-2").val() || ''
		let val3 = $("#input-3").val() || ''
		let val4 = $("#input-4").val() || ''
		let val5 = $("#input-5").val() || ''
		strCode = '' + val + val1 + val2 + val3 + val4 + val5
	}
    // 提交操作
	// ...
},

keydown(e) {
      var index = e.target.dataset.index * 1;
      var el = e.target;
			var ref = `#input-${index}`
      if (e.key === 'Backspace') {
				console.log($(ref).val())
        if ($(ref).val() !== '' && $(ref).val() !== undefined) {
          $(ref).val('')
        } else {
          if (el.previousElementSibling) {
            el.previousElementSibling.focus()
						var preRef = `#input-${index - 1}`
            $(preRef).val('')
          }
        }
      } else if (e.key === 'Delete') {
        if (($(ref).val()).length > 0) {
          $(ref).val('')
        } else {
          if (el.nextElementSibling) {
            $("#input-1").val('')
          }
        }
        if (el.nextElementSibling) {
          el.nextElementSibling.focus()
        }
      } else if (e.key === 'Home') {
        el.parentElement.children[0] && el.parentElement.children[0].focus()
      } else if (e.key === 'End') {
        el.parentElement.children[5] && el.parentElement.children[5].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 ($(ref).val() * 1 < 9) {
					let newVal = ($(ref).val() * 1 + 1).toString()
					$(ref).val(newVal)
        }
      } else if (e.key === 'ArrowDown') {
        if ($(ref).val() * 1 > 0) {
					let newVal = ($(ref).val() * 1 - 1).toString()
					$(ref).val(newVal)
        }
      } else if (e.key === 'Enter') {
        // 确认
				this.autoSubmodify()
      }
    },

keyup(e) {
      var index = e.target.dataset.index * 1;
      var el = e.target;
			var ref = `#input-${index}`
      if (e.code !== 'NumpadEnter' && e.code !== 'Enter' && parseFloat(e.target.value).toString() != "NaN") {
        el.nextElementSibling && el.nextElementSibling.focus();
        if (index === 5) {
					let val = $("#input-0").val()
					let val1 = $("#input-1").val()
					let val2 = $("#input-2").val()
					let val3 = $("#input-3").val()
					let val4 = $("#input-4").val()
					let val5 = $("#input-5").val()
          if (val && val1 && val2 && val3 && val4 && val5) {
						let code = '' + val + val1 + val2 + val3 + val4 + val5
						this.autoSubmodify(code)
          }
        }
      } else {
        if ($(ref).val() === '') {
          $(ref).val('')
        }
      }
    },

mousewheel(e) {
      var index = e.target.dataset.index;
			var ref = `#input-${index}`
      if (e.wheelDelta > 0) {
        if ($(ref).val() * 1 < 9) {
					let newVal = ($(ref).val() * 1 + 1).toString()
					$(ref).val(newVal)
        }
      } else if (e.wheelDelta < 0) {
				if ($(ref).val() * 1 > 0) {
					let newVal = ($(ref).val() * 1 - 1).toString()
					$(ref).val(newVal)
        }
      } else if (e.key === 'Enter') {
				let val = $("#input-0").val()
				let val1 = $("#input-1").val()
				let val2 = $("#input-2").val()
				let val3 = $("#input-3").val()
				let val4 = $("#input-4").val()
				let val5 = $("#input-5").val()
				if (val && val1 && val2 && val3 && val4 && val5) {
					document.activeElement.blur();
					let code = '' + val + val1 + val2 + val3 + val4 + val5
					this.autoSubmodify(code)
				}
      }
    },

// 复制粘贴
paste(e) {
			let _this = this
      // 当进行粘贴时
      e.clipboardData.items[0].getAsString(str => {
        if (str.toString().length) {
          let codeList = str.split('')
          document.activeElement.blur()
          codeList.forEach((num, index) => {
						$(`#input-${index}`).val(num)
					})
        }
				if(str.toString().length === 6) {
					_this.autoSubmodify(str)
				}
      })
    }

完!

相关推荐
breakthrough_0120 分钟前
创建一个简单的 Nuxt.js 应用
开发语言·javascript·ecmascript
2301_811274311 小时前
基于微信小程序的空巢老人健康管理系统
微信小程序·小程序
OEC小胖胖1 小时前
Vue 3 中 onUnload 和 onPageScroll 使用详解
前端·javascript·vue.js·前端框架·web
V+zmm101341 小时前
外卖商城平台的微信小程序ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·springboot
丁总学Java2 小时前
微信小程序加载商品首页数据时,页码没有更新,老是page=1。
微信小程序·小程序
秋田君2 小时前
uniapp中使用Mescroll实现下拉刷新与上拉加载项目实战
javascript·uni-app
川石教育3 小时前
Vue前端开发-slot传参
前端·vue.js·前端框架·前端开发·slot组件
确实菜,真的爱3 小时前
vue3 element plus 把表格数据导出到excel
javascript·vue.js·excel
一舍予3 小时前
nuxt3项目搭建相关
开发语言·javascript·vue.js·nuxt
新时代的弩力3 小时前
【Cesium】--viewer,entity,dataSource
前端·javascript·vue.js