uni-app框架h5页面实现pda扫条码将数值存入输入框

话不多说直接上代码 ,自己网上搜了半天然后拼拼凑凑搞出来的成功版本,还是萌新,有错轻喷 

<template>
  <view class="content">
    <input
      id="pdaInput"
      class="input"
      type="password"
      ref="searchInput"
      v-model="scanCodeValue"
      :placeholder="placeholder"
      :focus="focusState"
      @focus="focusFn"
      @blur="focusState = false"
    />
    <input
      class="input"
      type="text"
      :value="scanCodeValue"
      :placeholder="placeholder2"
    />
    <button @click="clear">clear</button>
  </view>
</template>

<script>
export default {
  data () {
    return {
      scanCodeValue: '',
      placeholder: '请扫码...',
      placeholder2: 's',
      focusState: false
    }
  },
  created () {
    // 抓取扫描枪数据
    document.onkeydown = (e) => {
      this.startScan()
    }
  },

  watch: {
    scanCodeValue (value) {
      if (!!value) {
        document.onkeydown = null
      } else {
        this.placeholder = '扫码失败,请重新扫码...'
      }
    }
  },

  destroyed () {
    //取消键盘监听事件
    document.onkeydown = null
  },
  methods: {
    focusFn () {
     // 聚焦后事件
    },
    clear () {
      this.scanCodeValue = ''
      document.onkeydown = (e) => {
        this.startScan()
      }
    },
    startScan () {
      this.$nextTick(() => {
        this.focusState = true
      })
      // 自动获取input输入框焦点
      if (e.which === 13) { // 安卓版PDA扫描枪扫描完后完成键的keycode=229
        // 其他类型可根据keycode值来确认哪一个是完成值,我的是13
        setTimeout(() => {
          if (this.scanCodeValue.length < 3) return
          this.scanCodeValue = ''
        }, 500)
        return
      }
    }
  }
}
</script>

<style>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.input {
  margin: 50px auto 0;
  padding: 3px;
  height: 30px;
  width: 200px;
  border: black 1px solid;
  border-radius: 2px;
}
</style>

最终效果如下,进入页面直接点pda上的扫描按钮对准条形码就能将数值扫进输入框,对应的绑定事件就可以根据业务需求来修改

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值