Vue Js Get Form Data on Submit:In Vue.js, you can get form data on submit by utilizing the v-model directive to bind form input fields to data properties. When the form is submitted, you can access the data properties directly to retrieve the form values. This approach ensures that the form data is automatically synchronized with the component’s data. By defining a method to handle the form submission, you can access the form data through the component’s data properties and perform any necessary actions or send it to a server. This streamlined process allows for efficient handling of form data in Vue.js applications.
How can I retrieve form data in Vue js when the form is submitted?
The provided code snippet demonstrates a Vue.js application that captures form data on submission. When the form is submitted, the handleSubmit method is triggered. Here’s a breakdown of the code:
- The Vue instance is created and attached to the element with the ID “ app“.
- The data object defines a property called
formDatathat contains two properties:nameandemail. These properties are bound to the respective input fields using thev-modeldirective, allowing two-way data binding. - The
handleSubmitmethod is defined within themethodsobject. It is called when the form is submitted. Theevent.preventDefault()prevents the default form submission behavior from occurring. - Inside the
handleSubmitmethod, the form data is accessed using object destructuring. Thenameandemailvariables are assigned the values fromthis.formData. - You can perform any necessary actions with the form data. In this example, the values are logged to the console using
console.log. - If you need to reset the form after submission, you can assign empty strings to the
nameandemailproperties offormData.
To summarize, this code captures form data using the v-model directive and retrieves the form data on form submission using the handleSubmit method.
Vue Js Get Form Data On Submit Example
<div id="app">
<form @submit="handleSubmit">
<div>
<label for="name">Name:</label>
<input type="text" id="name" v-model="formData.name" required>
</div>
<div>
<label for="email">Email:</label>
<input type="email" id="email" v-model="formData.email" required>
</div>
<button type="submit">Submit</button>
</form>
</div>
<script type="module">
const app = new Vue({
el: "#app",
data() {
return {
formData: {
name: '',
email: ''
}
};
},
methods: {
handleSubmit(event) {
event.preventDefault();
// Access the form data
const { name, email } = this.formData;
// Do something with the form data
console.log('Name:', name);
console.log('Email:', email);
// Reset the form if needed
this.formData.name = '';
this.formData.email = '';
}
}
});
</script>
Output of Vue Js Remove Duplicate Object from Array



