Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  176] [ 5]  / answers: 1 / hits: 99494  / 9 Years ago, fri, june 26, 2015, 12:00:00

I am using Vue.js in my app and have a text input within a form


<div id="myVueForm">
<form>
<input type="text" v-on="keyup:addCategory | key 'enter'">

<!-- more form fields -->
<button type="submit">Submit Form</button>
</form>
</div>

In my Vue instance I have the following


new Vue({
el: '#myVueForm',

methods: {
addCategory: function(e)
{
if(e) e.preventDefault();
console.log("Added a new category, thanks!");
}
}
});

Despite the preventDefault() call, when a user presses enter whilst on the text input the form still submits (although the addCategory() method does fire). This behaviour can be demonstrated in this fiddle.


I know I can use jQuery to catch the event and prevent the submit, but I'd like to see if it's possible in Vue to do this as it seems quite a common usage.


More From » html

 Answers
8

The submit is always fired on keydown. So use keydown instead of keyup.



<input type=text  v-on=keydown:addCategory | key 'enter'>

[#66035] Wednesday, June 24, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaceyr

Total Points: 510
Total Questions: 97
Total Answers: 116

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;