小程序输入框完成切换到下个输入框

该博客介绍了如何在微信小程序中实现多个输入框之间的焦点切换。通过监听input的bindconfirm事件,在用户输入完成时判断是否还有后续输入框,并设置相应的focus属性来控制焦点移动。同时,使用data-index绑定每个输入框的索引,以便在事件处理中准确地切换到下一个输入框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小程序输入框完成切换到下个输入框

需求:页面有多个输入框,一个输完点击回车,切换到下个输入框

思路:利用小程序输入框的focus(获取焦点)属性,在bindconfirm事件(点击完成后触发)里设置变量,来控制输入框聚焦位置。

1、wxml

<input data-index="{{index}}"  bindinput="inputListener" 
bindconfirm="confirmListener"  focus="{{focus && focusIndex == index}}"

2、js

Page({
  /**
   * 页面的初始数据
   */
  data: {
  	 // 是否获取焦点
    focus: false,
    // 需要获取焦点的序号
    focusIndex: 0
  },
  // 输入完成事件
  confirmListener (event) {
    let currentIndex = event.currentTarget.dataset.index
    if (currentIndex < input的数量 - 1) {
      this.setData({
        focus: true,
        focusIndex: currentIndex + 1
      })
    }else {
      this.setData({
        focus: false
      })
    }
  },
  // 输入时事件
  inputListener(event) {
    let currentIndex = event.currentTarget.dataset.index
    if (this.focusIndex != currentIndex) {
      this.setData({
        focusIndex: currentIndex
      })
    }
  },
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值