How to use the license plate number input method in the WeChat applet

hzc
Release: 2020-06-23 10:02:36
forward
4254 people have browsed it
When I was working on a small program, I made a project about cars, and then I needed to add vehicle information, add license plate numbers, and use the license plate keyboard to input. At that time, I cut off this requirement, and then when adding vehicle information, the boss saw I entered the license plate number randomly in the database and asked me to add it ^o^

1. Rendering

How to use the license plate number input method in the WeChat applet

How to use the license plate number input method in the WeChat applet

##2. Related code

  • Use component form to implement the keyboard Enter
component code index.wxml

<view>
  <block>
    <view>
      <view>{{item}}</view>
    </view>
    <view>
      <view>{{item}}</view>
    </view>
    <view>
      <view>{{item}}</view>
    </view>
    <view>
      <view>{{item}}</view>
    </view>
  </block>
  <block>
    <view>
      <view>{{item}}</view>
    </view>
    <view>
      <view>{{item}}</view>
    </view>
    <view>
      <view>{{item}}</view>
      <view>
        <image></image>
      </view>
    </view>
    <view>
      <view>{{item}}</view>
      <view>确定</view>
    </view>
  </block>
</view>
Copy after login
  • index.css
.carPlate{
  position: fixed;
  padding: 12rpx 12rpx 30rpx;
  left: 0;
  bottom: 0;
  width: 100%;
  /* height: 150px; */
  font-size: 30rpx;
  background: #fff;
  box-sizing: border-box;
  border-top: 1px solid rgb(211, 207, 207);
  z-index: 200;
}
.wordList{
  display: flex;
  width: 100%;
  justify-content: space-between;
  align-items: center;
}
.wordItem{
  margin: 5rpx;
  width: 70rpx;
  height: 70rpx;
  line-height: 70rpx;
  text-align: center;
  border: 1px solid #eee;
  border-radius: 10rpx;
}
.wordConfirm{
  width: 130rpx;
  color: #fff;
  background: #473af0;
}
.wordClear{
  width: 100rpx;
}
.clearImg{
  width: 60rpx;
  height: 60rpx;
  vertical-align: middle;
}
Copy after login
  • index.js
Component({

  properties: {
    type: {
      type: Number,
      default: 1,
    },
    show: {
      type: Boolean,
      default: false,
    }
  },

  data: {
    cityKeyword1: '京沪浙苏粤鲁晋冀豫',
    cityKeyword2: '川渝辽吉黑皖鄂湘赣',
    cityKeyword3: '闽陕甘宁蒙津贵云',
    cityKeyword4: '桂琼青新藏港澳台',
    keyNumber: '1234567890',
    wordList1: 'QWERTYUIOP',
    wordList2: 'ASDFGHJKL',
    wordList3: 'ZXCVBNM',
  },

  methods: {
    handleClick(e) {
      let value = e.currentTarget.dataset.item;
      let type = e.currentTarget.dataset.type;
      switch(value) {
        case 'confirm':
          this.triggerEvent('confirm');
          break;
        case 'delete':
          this.triggerEvent('delete');
          break;
        default: 
          this.triggerEvent('change', { value, type });
      }
    }
  }
})
Copy after login

3. Parent component introduction

  • I want to realize that after clicking input, there will be At first I wanted to use offset to achieve the pull effect, but after thinking about doing laundry after get off work, it was not easy to achieve. I then thought of the transform I used when I was making a shopping cart. The principle is similar, so I used it.
  • Then click outside the keyboard to close the keyboard. At first, what I thought of was to define the closing event at the outermost layer of the parent component, and use the catch method to block the boxes in the parent. Bubbling, but it seemed a bit unreasonable to prevent bubbling, so I removed the blocking bubbling
  • parent component index.wxml
<view>
  <view>
    <view>
      <view>
        <view>*车牌号码</view>
        <view>
          <view>{{carNo}}</view>
          <view>请输入车牌号</view>
        </view>
      </view>
    </view>
  </view>
</view>
<car-plate></car-plate>
Copy after login
  • Parent component index.js
Page({
  data: {
    carNo: '',
    translateSpace: 0,
    inputType: 1, // 车牌输入类型,1简称,2数字或者字母,
    showPlateInput: false,
  },
  /* 用于点击弹出键盘输入,space为键盘弹出后向上拉取的距离 */
  handleClick(e) {
    /* 150为键盘的高度 */
    let space = -(e.currentTarget.offsetTop - 150);
    /* regExp用于判断当前已输入的车牌号是否是中文,并让键盘显示中文还是英文输入 */
    let regExp = /^[\u4e00-\u9fa5]+/;
    let inputType = 1;
    if(regExp.test(this.data.carNo)) {
      inputType = 2;
    }

    this.setData({
      translateSpace: space,
      showPlateInput: true,
      inputType
    })
  },
  /* 键盘输入操作 */
  handlePlateChange(e) {
    let value = e.detail.value;
    let type = e.detail.type;
    let carNo = this.data.carNo;
    carNo += value;

    if(type == 1) {
      this.setData({
        inputType: 2
      })
    }
    this.setData({
      carNo
    })
  },
  /* 点击键盘上的确定 */
  handlePlateConfirm() {
    /* isCarPlate用于判断输入的车牌号是否符合规范 */
    if (!this.isCarPlate(this.data.carNo)) {
      wx.showToast({
        title: '请输入正确的车牌号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      translateSpace: 0,
      showPlateInput: false,
      inputType: 1
    })
  },
  /* 用于键盘输入删除 */
  handlePlateDelete(e) {
    let carNo = this.data.carNo;
    carNo = carNo.substring(0, carNo.length - 1);
    if(carNo.length == 0) {
      this.setData({
        inputType: 1
      })
    }
    this.setData({
      carNo,
    })
  },
  /* 判断车牌号 */
  isCarPlate(value) {
    return /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(value);
  }
})
Copy after login
  • Parent component index.css
.container{
  height: 100vh;
  background: #fff;
}
.translateView{
  background: #eee;
}
.list{
  margin-bottom: 20rpx;
  background: #fff;
}
.list:last-child{
  margin: 0;
}
.item{
  display: flex;
  padding: 0 26rpx;
  width: 100%;
  height: 116rpx;
  box-sizing: border-box;
  align-items: center;
  border-bottom: 1px solid #eee;
}
.item:last-child{
  border: none;
}
.label{
  margin-right: 10rpx;
  width: 140rpx;
}
.contentBox{
  display: flex;
  width: calc(100% - 150rpx);
  height: 90rpx;
  align-items: center;
  justify-content: space-between;
}
.promptText{
  color: #c7c7c7;
}
.inputBox{
  width: 100%;
  height: 80rpx;
  line-height: 80rpx;
}
Copy after login
I am studying hard. If it is helpful to your study, please leave your mark (like it^_^)
Recommended tutorial: "

WeChat Mini Program"

The above is the detailed content of How to use the license plate number input method in the WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template