微信小程序实现联动删除输入验证码框

以下是json代码

bash 复制代码
{
  "component": true,
  "usingComponents": {}
}

以下是wxml代码

bash 复制代码
<van-popup show="{{ show }}" bind:close="onClose"   custom-class="extract">
  <image  src="../../images/extract/icon1.png" mode=""/>
  <view class="title">请输入验证码</view>
  <view class="ys-verification">
    <view class="input-wrapper" wx:for="{{amount}}" wx:key="index">
      <input type="number" value="{{code[index]}}" data-index="{{item}}" title="code" focus="{{item == currentIndex}}" maxlength="1" data-index="{{index}}" bindinput='handleInput'/>
    </view>
  </view>
  <view class="tips">
    请输入验证码
  </view>
  <view class="btn">提交</view>
</van-popup>

以下是css代码

bash 复制代码
.ys-verification {
  width: 100%;
  height: 100rpx;
  display: flex;
  justify-content: space-around;
  margin-top: 34rpx;
  padding-top: 48rpx;
  margin-bottom: 24rpx;
  border-top: 2rpx solid #FFFFFF;
}
.ys-verification .input-wrapper {
  width: 102rpx;
  height: 100rpx;
  background: #FFFFFF;
  border-radius: 8rpx;
  position: relative;
}
.ys-verification input {
  position: absolute;
  width: 100%;
  height: 100%;
  text-align: center;
  font-size: 50rpx;
  color: #333;
  background: #fff;
}
.extract {
  width: 622rpx;
  height: 490rpx;
  background: linear-gradient(135deg, #E1FFFB 0%, #FFE6E6 100%);
  border-radius: 8rpx;
  padding: 32rpx;
  position: relative;
}
.extract > image {
  position: absolute;
  width: 36rpx;
  height: 36rpx;
  top: 36rpx;
  right: 36rpx;
}
.extract .title {
  font-size: 32rpx;
  color: #41475B;
  text-align: center;
}
.extract .tips {
  font-size: 28rpx;
  color: #41475B;
  text-align: center;
}
.extract .btn {
  width: 304rpx;
  height: 84rpx;
  text-align: center;
  line-height: 84rpx;
  background: #05C8AF;
  border-radius: 12rpx ;
  font-size: 28rpx;
  color: #FFFFFF;
  margin: 48rpx auto 0;
}

以下是js逻辑代码

bash 复制代码
Component({
  /**
   * 组件的属性列表
   */
  properties: {
 
 
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:1,
    code:[],
    currentIndex:0,
    amount:4
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClose(e){
this.setData({
  show:0
});
    },
    handleInput(e){
      let value = this.validateNumber(e.detail.value);
      let index = e.currentTarget.dataset.index;
      const oldValue = this.data.code[index];
      if(value!==''){
        let code = this.data.code;
        code[index] = value;
        this.setData({
          code,
          currentIndex: ++index
        });
        if(!code.includes('')){
          this.triggerEvent('onCompleted',{code: code.join('')},{
            bubbles: true,
            composed: true
          })
        }
      }else{
        const isDeleted = oldValue !== ''; // 但是无法监听当value为''的情况,因为不会触发input事件-->
        let code = this.data.code;
        code[index] = '';
        this.setData({
          code,
          currentIndex: isDeleted?--index:index
        })
      }
    },
    validateNumber(val) {
      return val.replace(/\D/g, '')
    },
  },
  attached() {

  }
})
相关推荐
EasyNVR2 小时前
基于WebRTC与AI大模型接入EasyRTC:打造轻量级、高实时、强互动的嵌入式音视频解决方案
运维·服务器·微信·小程序·webrtc·p2p·智能硬件
我命由我123452 小时前
微信小程序 - 自定义实现分页功能
前端·微信小程序·小程序·前端框架·html·html5·js
HappyAcmen13 小时前
关于微信小程序的面试题及其解析
微信小程序·小程序·notepad++
乔冠宇13 小时前
微信小程序修改个人信息头像(uniapp开发)
微信小程序·小程序·uni-app
lvbb6614 小时前
微信小程序-路线规划功能
微信小程序·小程序·notepad++
爱上大树的小猪16 小时前
微信小程序模仿快播标签云滚动特效
微信小程序·小程序
從南走到北21 小时前
挪车小程序挪车二维码php+uniapp
微信小程序·小程序·开源·微信公众平台
黑云压城After21 小时前
uniapp小程序自定义日历(签到、补签功能)
小程序·uni-app
黑云压城After1 天前
小程序(物流、快递),接入GPS北斗获取路线以及当前车辆位置
小程序
万岳科技程序员小金1 天前
互联网医院系统源码解析:如何开发智能化的电子处方小程序?
小程序·app开发·互联网医院系统源码·智慧医疗小程序·医院app