uniapp中如何实现自动让input文本框聚焦

346 阅读1分钟

在开发过程中遇到这样一个问题,利用PDA扫码,扫码后文本框的焦点会自动失焦,但是又想让自动获取焦点连续扫码,那该如何做呢?请往下看: 也许大部分同学和我一样都会这么做,一直修改文本框属性focustrue,但是没有效果,参考如下

<view class="" style="display: inline-block; width: 80%;">

					<input
					v-model="barCode" 
					 @blur="shiquBlur()"
					@confirm="getScanValue()"
					:showAction="false"  
					:focus="focusShow"
					id="bar_name"
					class="barInput" 
					:placeholder="placeholderValue" 
					:animation="false"></input>
  </view>
  
  focusShow: true

尝试手动实现,调用focus事件,想到它可不像之前的h5项目,是没有操作dom的方法,所以暂时搁置,在网上找了解决方案都不合适

最后理清了一下思路得使focus的状态更新才能生效,添加了onblur事件

shiquBlur() {
  let that = this
  that.focusShow = false // 这个代码是关键!!!!
  that.$nextTick(() => {
	that.focusShow = true
   })
   that.$forceUpdate()
},