小程序切换到下一个输入框

2020-04-09  本文已影响0人  清蒸鱼跃龙门

js:

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

wxml:

<input id="input{{index}}" data-category-index="{{index}}"  
bindinput="inputListener" bindconfirm="confirmListener"  
focus="{{focus && focusIndex == index}}"
上一篇 下一篇

猜你喜欢

热点阅读