Submit a form in vue. How do I reference the form element?
Sorry for late you answer. but i am confusing why you need ref when form submission,
data: function(){
return {
name: "",
price: "",
}
},
methods: {
addProduct(e){
e.preventDefault() // it prevent from page reload
// console.log(this.name, this.price);
}
}
<form v-on:submit="addProduct">
<input type="text" v-model="name" placeholder="Product Name" >
<input type="number" v-model="price" placeholder="Price" >
<button type="submit">Add</button>
</form>
Another option is trigger the event click()
.
<button ref="submit" style="display:none;">Submit</button>
In your function call it as follows:
this.$refs.submit.click();
The answer would be: ref
: https://v2.vuejs.org/v2/api/#ref
Markup
<div id="vueRoot">
<form ref="form">
<input name="vitalInformation" v-model="store.vital">
<a href="#" v-on:click="submit">SUBMIT</a>
</form>
</div>
code
var store = {vital:''};
vm = new Vue({
el : "#vueRoot",
data : {store : store},
methods : {
submit : function(){
this.$refs.form.$el.submit()
}
}
});