Vue、el-table内嵌el-input调用 focus 方法无效;this.$refs.xxx.focus()方法无效

1、需求:

在表格编辑态的时候,可以在①处敲击“回车键”,光标能跳转到②处

2、代码片段(这种方式不生效,失效)

显示页面代码:

<el-table-column label="尺寸厚度x1" align="center" min-width="120" prop="sizeCheckResult" sortable="custom">
  <template slot-scope="scope">
     <el-input v-model="scope.row.sizeCheckResult" :ref="scope.$index + 'sizeCheckResult'"  @keyup.native.enter="focusNextInput('sizeCheckResult2',scope.$index)" placeholder="" class="w_100"></el-input>
  </template>
</el-table-column>

methods方法:

//回车后聚焦另一个输入框
focusNextInput(nextRef,index) {
   var nextInput = index + nextRef;
   this.$refs[nextInput].focus()
}

3、分析原因

试了一下,当不把<el-input>标签放在<el-table>里的时候,比如正常表单(ref不是通过动态赋值)的时候,这段代码是完全没有问题的。所以问题应该是出在了<el-table>上,不知道Element-ui加了什么限制或者Vue的什么bug,导致的代码执行了,但是光标却没有变化

4、改为原生DOM的方式(自测生效)

页面显示代码:(将 ref 改为 id,其他都没变)

<el-table-column label="尺寸厚度x1" align="center" min-width="120" prop="sizeCheckResult" sortable="custom">
  <template slot-scope="scope">
    <el-input v-model="scope.row.sizeCheckResult" :id="scope.$index + 'sizeCheckResult'" @keyup.enter.native="focusNextInput('sizeCheckResult2',scope.$index)" placeholder="" class="w_100"></el-input>
  </template>
</el-table-column>

methods方法:(将this.$refs 改为 document.getElementById)

//回车后聚焦另一个输入框
focusNextInput(nextRef,index) {
  var nextInput = index + nextRef;
  document.getElementById(nextInput).focus();
}

5、总结:

至于为啥this.$refs.xxx.focus() 这个方法不生效,目前还没有研究出来,有知道的大佬欢迎留言在这篇文章的评论区,共勉!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值