2016-10-02 125 views
3

我有2個輸入,當用戶按Enter鍵時,需要從第一個焦點到第二個焦點。 我試着搭配的jQuery與Vue公司becouse我找不到任何功能,專注在某件事Vue的文檔中:Vue JS將焦點放在輸入的下一個輸入

<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 
+0

我不知道答案,但請儘量在V-這個':keyup.enter = 「this.nextSibling.focus()」' – Robiseb

+0

感謝您的幫助!結果是'build.js:15333 Uncaught TypeError:無法讀取未定義的屬性'焦點' – user3479125

+0

您的第二個輸入有一個ID? ''。嘗試添加一個並更改此行'v-on:keyup.enter =「document.getElementById('XX')。focus()」' – Robiseb

回答

6

你可以嘗試這樣的事情:

v-on:keyup.enter="$event.target.nextElementSibling.focus() 

JSfiddle

2

一些測試後,它的工作

new Vue({ 
 
    el:'#app', 
 
    methods: { 
 
     nextPlease: function (event) { 
 
     document.getElementById('input2').focus(); 
 
     } 
 
    } 
 
});
<script src="https://vuejs.org/js/vue.js"></script> 
 
<div id='app'> 
 
    <input type="text" v-on:keyup.enter="nextPlease"> 
 
    <input id="input2" type="text"> 
 
</div>

+0

是的,謝謝,我幾分鐘前意識到將代碼移到方法的作品 – user3479125