uniapp 扫描枪获取条码不全解决办法,vue组件,使用双向绑定,回车事件触发,获取文本不全问题

原因分析:

uniapp使用vue,双向绑定组件,在input标签loop事件循环获取文本的过程中,读取文本信息不是顺序读取,如果条码中包含了回车键,比如扫描枪,扫描文本后会追加回车键,监控文本读取还没有读取完所有文本,先读取到了回车,触发了相应的回车事件导致
关键点:confirm回车事件先于 data属性值绑定,事件触发时,data值还没有完全绑定完
关键代码:在input标签中传递事件对象,通过事件多谢直接获取值,此时值是完整的
在这里插入图片描述
在这里插入图片描述

BinderConfirm(e) {
				let binderCode = e.target.value
				// console.log(binderCode)
				this.tetsCode.binderText = this.tetsCode.binderText + "\n" + binderCode
				this.blurfun();
			}
unBinderConfirm(e) {
				//获取文本框条码
				let text = e.target.value
				// this.barCode = e
				// console.log(text)
				this.tetsCode.unbinderText = this.tetsCode.unbinderText + "\n" + text
			}

解决方案

1.不使用双向绑定

uniapp使用vue,对于扫码后,数据的重置空,不使用绑定,很难操作,第一种方案的清空重置,在html5上可以使用,在打包apk后无法使用,相对不完美

2.使用双向绑定

相对第一种更方便对数据进行清理重置,比较符合不停扫码的场景

在这里插入图片描述

上代码,barcodeTest.vue

<template>
	<view class="uni-common-mt">
		<view class="uni-padding-wrap">
			<view class="cu-form-group">
				<view class="title">未绑定2:</view>
				<input class="input" type="text" :focus="tetsCode.isfocus2" @blur="tetsCode.isfocus2 = false" id="codeTest1" ref="inputCode" @confirm="unBinderConfirm($event)"></input>
			</view>
			<view class="uni-textarea" style="background-color: white;">
				<textarea v-model="tetsCode.unbinderText" style="color: black;" />
			</view>
			<view class="tag-view panel-full margin-top">
				<uni-tag text="清空条码" type="success" @click="clearBarCode()" />
			</view>
			<view class="tag-view panel-full margin-top">
				<uni-tag text="清空" type="success" @click="tetsCode.unbinderText = '未绑定文本:'" />
			</view>
			<view class="cu-form-group">
				<view class="title" style="color: red;">双向绑定:</view>
				<input ref="testBinderCode" v-model="tetsCode.binderCode" :focus="tetsCode.isfocus" @confirm="BinderConfirm($event)" class="input" type="text"
					name="codeTest2" style="color: red;" @blur="blurfun()"></input>
			</view>
			<view class="uni-textarea" style="background-color: white;">
				<textarea  v-model="tetsCode.binderText" style="color: red;" />
			</view>
			<view class="tag-view panel-full margin-top">
				<uni-tag text="清空" type="success" @click="tetsCode.binderText = '双向绑定文本:'" />
			</view>
			<view class="tag-view panel-full margin-top">
				<uni-tag text="清空绑定条码" type="success" @click="setValue2()" />
			</view>
			<view class="cu-form-group">
				<view class="title" style="color: green;">主机名:</view>
				<input v-model="tetsCode.host"  class="input" type="text" name="codeTest2"
					style="color: green;"></input>
			</view>
			<view class="tag-view panel-full margin-top">
				<uni-tag text="获取" type="error" @click="download3()" />
			</view>
		
		</view>
	</view>
	</view>
</template>

<script>
	import permision from "@/common/js/permission.js"
	import uniTag from '@/components/uni-ui/uni-tag/uni-tag.vue';
	import UniIcons from '@/components/uni-ui/uni-icon/uni-icon.vue'
	import UniNumberBox from '@/components/uni-ui/uni-number-box/uni-number-box.vue'

	import uniIcon from '@/components/uni-icon/uni-icon.vue'
	import tTable from '@/components/t-table/t-table.vue';
	import tTh from '@/components/t-table/t-th.vue';
	import tTr from '@/components/t-table/t-tr.vue';
	import tTd from '@/components/t-table/t-td.vue';
	

	export default {
		components: {
			uniTag,
			UniIcons,
			UniNumberBox,
			uniIcon,
			tTable,
			tTh,
			tTr,
			tTd
		},
		onLoad(e) {

		},
		data() {

			return {
				tetsCode: {
					binderCode: "123",
					binderText: "双向绑定文本:",
					unbinderText: "未绑定文本:",
					host: "172.30.162.129",
					isfocus:true,
					isfocus2:false,
					barCode:"132"
				}
			}
		},
		onLoad() {
			//this.circleRodObjList = []
		},
		methods: {
			unBinderConfirm(e) {
				//获取文本框条码
				let text = e.target.value
				// this.barCode = e
				// console.log(text)
				this.tetsCode.unbinderText = this.tetsCode.unbinderText + "\n" + text
			},
			BinderConfirm(e) {
				let binderCode = e.target.value
				// console.log(binderCode)
				this.tetsCode.binderText = this.tetsCode.binderText + "\n" + binderCode
				this.blurfun();
			},
			clearBarCode(){
				this.$refs.inputCode.valueSync = ""
				this.tetsCode.isfocus2 = true
				// this.barCode.target.text = ""
				// var obj = document.getElementById('codeTest1') ;
				// obj.outerText = ""
				// obj.focus()
				// obj.outerHTML="";
				// const query = uni.createSelectorQuery().in(this);
				// let select = query.select('#codeTest1')
				// select._component.$refs.inputCode.valueSync = ""
				// query.select('#codeTest1').boundingClientRect(data => {
				// 	console.log(data)
				// }).exec();

			}
			,
			download3() {
				let downUrl = "http://" + this.tetsCode.host + ":8088/webapp/service/apk/downloadLastFile2"
				plus.runtime.openURL(downUrl)
			},
			setValue(){
				let input = this.$refs['inputCode'];
				let value = input.valueSync
				// let value = "aaaa"+input.toString()
				// uni.showModal({
				// 	title: "refs对象",
				// 	duration: 1000,
				// 	content: value,
				// 	showCancel: false,
				// 	success() {
				// 		uni.hideLoading()
				// 	}
				// })
				input.valueSync = ""
				// input.focus = true
				 // var obj = document.getElementById("codeTest1");
				 // obj.valueSync = ""
				// this.barCode = "";
				// var obj = document.getElementById('codeTest1') ;
				// obj.focus()
				// obj.value = ""
			},
			// onInput(e){
			// 	// this.barCode.target.value = e.target.value
			// }
			setValue2()
			{
				this.tetsCode.binderCode = ""
				this.tetsCode.isfocus = true
			},
			blurfun(){
				this.tetsCode.isfocus = false
			}
		}
	}
</script>
<style>
	.uni-padding-wrap {
		width: 100%;
		padding: 0 20upx;
	}

	.uni-common-mt {
		width: 100%;
	}

	.cu-form-group {
		width: 100%;
		margin: 20upx 0;
	}

	.scroll-view_H {
		width: 200;
		display: flex;
		flex-wrap: nowrap;
	}

	.scrollx_items {
		text-align: center;
		display: inline-block;
		width: 210rpx;
		box-sizing: border-box;
		margin-left: 30rpx;
		margin-top: 3px;
	}

	.t-td {

		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;

	}

	.t-th {
		width: 180px;
	}

	.tap-button-text {
		color: darkcyan
	}

	.radio_mat {

		margin-left: 40upx;
	}
</style>

3.修改双向绑定策略,或者自己封装双向绑定组件解决问题,由于时间问题,暂未实现

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JerryLXu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值