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

以下是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() {

  }
})
相关推荐
游戏开发爱好者84 分钟前
iOS 性能测试的工程化方法,构建从底层诊断到真机监控的多工具测试体系
android·ios·小程序·https·uni-app·iphone·webview
计算机毕设指导615 分钟前
基于Springboot+微信小程序流浪动物救助管理系统【源码文末联系】
java·spring boot·后端·spring·微信小程序·tomcat·maven
2501_9160088942 分钟前
iOS App 混淆的真实世界指南,从构建到成品 IPA 的安全链路重塑
android·安全·ios·小程序·uni-app·cocoa·iphone
计算机毕设指导61 小时前
基于微信小程序的健康指导平台【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
wx_ywyy67981 小时前
同城跑腿小程序核心功能解析:多订单合并、智能计费与实时位置共享
小程序·跑腿小程序·小程序制作·同城小程序·同城服务小程序开发·跑腿小程序开发·外卖小程序开发
2501_915106321 小时前
iPhone 耗电异常全面诊断指南,构建多工具协同的电量分析与优化体系
android·ios·小程序·https·uni-app·iphone·webview
han_hanker1 小时前
svg 可改颜色,但似乎不支持微信小程序
微信小程序·小程序·notepad++
2501_915909063 小时前
Fiddler抓包与接口调试实战,HTTPHTTPS配置、代理设置与移动端抓包详解
前端·测试工具·ios·小程序·fiddler·uni-app·webview
我命由我123454 小时前
微信小程序开发 - 为 tap 事件的处理函数传递数据
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
2501_9151063212 小时前
如何查看手机使用记录:Android和iOS设备全面指南
android·ios·智能手机·小程序·uni-app·iphone·webview