我有2个输入,当用户按下回车键时,我想把焦点从第一个切换到第二个.

<input v-on:keyup.enter="$(':focus').next('input').focus()"
    ...>
<input  ...>

但在进入控制台时,我看到错误:

build.js:11079 [Vue warn]: Property or method "$" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in anonymous component - use the "name" option for better debugging messages.)warn @ build.js:11079has @ build.js:9011keyup @ build.js:15333(anonymous function) @ build.js:10111
build.js:15333 Uncaught TypeError: $ is not a function

推荐答案

你可以试试这样:

<input v-on:keyup.enter="$event.target.nextElementSibling.focus()" type="text">

100

更新

如果目标元素位于form元素内,而下一个表单元素不是真正的同级元素,则可以执行以下操作:

html

<form id="app">
  <p>{{ message }}</p>

  <input v-on:keyup.enter="goNext($event.target)" type="text">
  
  <div>
      <input type="text">
  </div>

</form>

js

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!',
    focusNext(elem) {
      const currentIndex = Array.from(elem.form.elements).indexOf(elem);
      elem.form.elements.item(
        currentIndex < elem.form.elements.length - 1 ?
        currentIndex + 1 :
        0
      ).focus();
    }
  }
})

100

Vue.js相关问答推荐

Vuetify从同一方向旋转传送带下一个项目和上一个项目

Nuxt.js i18n不提供默认区域设置中的页面而不重定向

在 Nuxt3 中使用 Vue3 vue-ganntastic 插件

传单 vue3 组件变体打开错误的标记 - 单击时弹出

如果单元格不可见,则以编程方式打开行的编辑模式. Ag 网格,Vue.js

单击 v-data-table 中行中的任意位置并移动到另一个页面

如何在 vue 组件中定义变量? (Vue.JS 2)

VuetifyJS:如何摆脱 v-stepper 组件的提升?

如何在 vue.js 中删除类

在按键 vuejs 中只允许数字和一个小数点后 2 位限制

如何在 vue.js 中包含当年的版权?

为什么当我 Select 第二个输入框时音译transliteration不起作用?

如何在 vue 3 脚本设置中使用

VueJS 禁用特定属性的react性

Amazon EC2 错误:监听 EACCES 0.0.0.0:80

vue-test-utils:无法覆盖属性 $route,这通常是由于插件将该属性添加为只读值

Vue更改宽度和内容

使用 vue-router 的默认子路由

如何在 VueJS 中下载本地存储的文件

如何使用 Vue Test utils 测试输入是否聚焦?