小程序多个输入框输入验证码功能 实现

老套路,先上图 

<view class="check">
  <!-- 输入核验码 -->
  <view class="query">
    <view class="query_item_name">请输入核验码</view>
    <view class="query_num_block">
        <input type='number' class="num_item_block" wx:for="{{inputLen}}" wx:key="{{index}}" disabled bindtap='onFocus' value="{{iptValue.length>=index+1?iptValue[index]:''}}" />
    </view>
    <input name="password" password="{{true}}" class='hidden_ipt' maxlength="{{inputLen}}" focus="{{isFocus}}" bindinput="setValue"></input>
    <view class="confirm" bindtap='examine'>确认</view>
  </view>
</view>

 

.query{
  padding-top:90rpx;
}
.query .query_item_name{
  font-family: PingFangSC-Regular;
  font-size: 30rpx;
  color: #737586;
  letter-spacing: 0;
  text-align: center;
}
.query_num_block{
  display: -webkit-flex;
  display: flex;
  justify-content: space-between;
  padding-left:36rpx;
  padding-right:36rpx;
  margin:80rpx auto 110rpx;
}
.check .confirm{
  margin:auto;
  width:200rpx;
  height: 68rpx;
  background: #F86221;
  border-radius: 33px;
  text-align: center;
  line-height: 68rpx;
  font-family: PingFangSC-Medium;
  font-size: 30rpx;
  color: #FFFFFF;
  letter-spacing: 0;
}
.num_item_block{
  height: 116rpx;
  width:80rpx;
  border:1px solid #cdcdcd;
  font-family: PingFangSC-Regular;
  border-radius: 8rpx;
  line-height: 116rpx;
  text-align: center;
  font-size: 36rpx;
  color: #25252B;
}
.hidden_ipt{
  height: 0rpx;
  width:0rpx;
  border:none;
  margin:0;
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputLen: 7,
    iptValue: "",
    isFocus: false,
  },
  onFocus: function (e) {
    var that = this;
    that.setData({ isFocus: true });
  },
  setValue: function (e) {
    var that = this;
    that.setData({ iptValue: e.detail.value });
  },
})

 

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
实现输入框短信验证码功能,可以使用 Vue.js 的模板语法和组件化思想,具体实现步骤如下: 1. 创建一个包含输入框和发送验证码按钮的组件。 ```html <template> <div> <input type="text" v-model="code" /> <button @click="sendCode" :disabled="disabled">{{ buttonText }}</button> </div> </template> <script> export default { data() { return { code: '', disabled: false, buttonText: '发送验证码', countdown: 60 } }, methods: { sendCode() { // 发送短信验证码 // ... // 设置倒计时 this.disabled = true; this.buttonText = `${this.countdown}s 后重新发送`; const timer = setInterval(() => { this.countdown--; this.buttonText = `${this.countdown}s 后重新发送`; if (this.countdown === 0) { clearInterval(timer); this.disabled = false; this.buttonText = '发送验证码'; this.countdown = 60; } }, 1000); } } } </script> ``` 2. 在父组件中引入该组件,并监听子组件的输入事件。 ```html <template> <div> <sms-code @input="handleInput" /> <button @click="submit">提交</button> </div> </template> <script> import SmsCode from './SmsCode.vue'; export default { components: { SmsCode }, data() { return { phone: '', code: '' } }, methods: { handleInput(value) { this.code = value; }, submit() { // 提交表单 // ... } } } </script> ``` 在子组件中,使用 `v-model` 来实现与父组件的双向数据绑定。当输入框的值发生变化时,通过 `$emit` 方法将新的值传递给父组件。 ```html <template> <div> <input type="text" v-model="code" @input="$emit('input', code)" /> <button @click="sendCode" :disabled="disabled">{{ buttonText }}</button> </div> </template> <script> export default { data() { return { code: '', disabled: false, buttonText: '发送验证码', countdown: 60 } }, methods: { sendCode() { // 发送短信验证码 // ... // 设置倒计时 this.disabled = true; this.buttonText = `${this.countdown}s 后重新发送`; const timer = setInterval(() => { this.countdown--; this.buttonText = `${this.countdown}s 后重新发送`; if (this.countdown === 0) { clearInterval(timer); this.disabled = false; this.buttonText = '发送验证码'; this.countdown = 60; } }, 1000); } } } </script> ``` 这样就可以实现一个简单的输入框短信验证码功能了。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陨石猎人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值