elementUI el-input focus

  • Q1. el-input 获取焦点
  • Q2. dialog中的 el-input获取焦点
  • Q3. dialog中有table table中有 el-input 要获取焦点

一个宗旨: this.$refs.XXX.$el.querySelector('input').focus();

answer 1

 <el-input  ref="mark"></el-input> 

使用时直接 (对于多个el-input也是一样的)

this.$refs.mark.$el.querySelector('input').focus();

answer 2

需要在dialog打开时候让input获取焦点

<el-dialog
      title="销售员"
      :visible.sync="customerVisible"
      @open="customerDialogOpen"  // 这个是重点 > <el-input ref="customerInput" ></el-input> </el-dialog> 
//销售员 dialog 打开时 获取焦点
      customerDialogOpen() {
        this.customerVisible = true
        this.$nextTick(function () { this.$refs.customerInput.$el.querySelector('input').focus(); }); }, 

answer 3

 <el-dialog title="结账" :visible.sync="sumVisible" :close-on-click-modal="false" @open="sumDialogOpen"> <el-table :data="tableData" size="mini" style="width: 100%"> <el-table-column prop="code" label="编号" width="50"> </el-table-column> <el-table-column prop="way" label="结算方式" width="80"> </el-table-column> <el-table-column label="金额"> <template slot-scope="scope"> <el-input size="mini" :ref="scope.row.ref" //看这里看这里 @keyup.up.native="up2pre(scope.row.ref)" @keyup.down.native="down2next(scope.row.ref)"> </el-input> </template> </el-table-column> ... </el-table> </el-dialog> 
tableData : [
  {
    code: '01',
    way: '现金',
    disabled: true,
    ref: 'input1',
  }, {
    code: '02', way: '银行卡', disabled: false, ref: 'input2', } ] 

下面就清楚了吧,跟上面2 的套路一样

//结算 dialog 打开时 获取焦点
      sumDialogOpen() {
        this.sumVisible = true
        this.$nextTick(function () { this.$refs.input2.$el.querySelector('input').focus(); }); }, 

至于多个input之间焦点如何切换,

down2next(e) {
        let input
        switch (e) {
          case "input1": input = `input2` break case "input2": input = `input3` break } this.$refs[input].$el.querySelector('input').focus(); }, 

各位兄台有没有便捷的方法?总感觉这样写的好蠢 啊,:smile:



作者:小学生的博客
链接:https://www.jianshu.com/p/63bfbbbd1e82
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值