Extract form data from an array using Vue.js

I'm facing a challenge with a form that displays multiple inputs based on database data. When I submit the form, I need to capture the value of each input separately.

Code

<form ref="form" :model="form">
    <div class="row">
        <div
        class="col-md-6"
        v-for="(field, index) in fields"
        :key="index"
        >
        <input
            class="form-control"
            v-model="form.field"
            :placeholder="field.title"
        />
        </div>
    </div>
    <vs-button
        class="mt-3"
        @click="onSubmit"
        native-type="submit"
        gradient
    >
        Generate
    </vs-button>
</form>


data() {
    return {
      fields: [],
      form: {
        field: [],
      },
    };
},

Issue

The current issue is that when I enter a value in one input, all the other inputs get the same value. I need each input to retain its own unique value.

Screenshots

https://i.sstatic.net/ndIQP.png

https://i.sstatic.net/obZlJ.png

Any suggestions?

Answer №1

Instead of using v-model with just form.field, consider using

v-model="form.field[index]"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to retrieve every element inside a specific div container

Is there a way to select all elements inside a div except for specific ones? For example, consider the following structure: <div id="abc"> <div class="def"> sagar patil</div> <div class="pqr"> patil</div& ...

"Using a function parameter as a key in a JSON object

While I may not be a JSON expert, I am struggling to understand why this code is not functioning as expected and what steps I need to take to fix it. Despite my efforts to search for solutions, I suspect that my lack of familiarity with the correct termino ...

Removing annoying padding from styled select in IE11/Windows 8 - a step by step guide

The select element is hidden behind the text using opacity: 0 and will only be displayed upon clicking. However, I am encountering an issue with unwanted padding on the top and bottom of the list in IE11 on Windows 8: https://i.sstatic.net/73yjO.png This ...

When utilizing the 'import * as' statement in a TypeScript component, it results in a erroneous outcome -

Attempting to incorporate an HTML template into a TypeScript component is resulting in an error for me. This is being done in Angular 1.5. This is how the component appears... import * as template from './home.template.html'; import { HomeContr ...

When Firebase authentication signs out users who were previously authenticated, it results in them facing permission denied errors

1) User A visits my website, and A is successfully authenticated and can write to the firebase database through the web browser. 2) User B then visits my site, and after being authenticated, they are able to write to the firebase database via the browser. ...

Encountering difficulties with showing contact images in phonegap using angularjs

In my project, I encountered an issue where I can fetch and display the contact photo using simple HTML and JavaScript. However, when I attempt to do the same using AngularJS model, I encounter an error. Below is the code snippet that I am struggling with: ...

Using Node.js to send instructions to an HTTP server in order to highlight or add color to specific elements within

I have set up a server that receives data from a gaze sensor, as well as an HTTP server that serves an HTML page. I am looking for a way to dynamically highlight elements in the HTML based on the incoming data. Any suggestions on what resources or techniqu ...

"Looking to reset timers on multiple items using the jQuery off handler? Here's how to do

// The Javascript Code var timer; $(document).ready(function () { $("li").hover( function () { clearTimeout(timer); $(this).css("background-color", "red"); }, function () { var $self = $(this); timer = setTimeout(func ...

Set the mat-option as active by marking it with a check symbol

Currently, I am utilizing mat-autocomplete. Whenever a selection is made manually from the dropdown options, the chosen item is displayed with a distinct background color and has a checkmark on the right side. However, when an option in the dropdown is se ...

Using Plotly.js within a deleted Vue.js component may result in displaying an incorrect chart

Issue I am facing a problem with deleting one of the components that contains a chart. Even after deleting the component, the chart remains visible. To see an example, check out this jsfiddle: https://jsfiddle.net/m4ywp5fc/4/ Solution Attempted I attem ...

Having difficulty applying capitalization to the initial word in an input using JavaScript/jQuery

I've read through several other discussions on this forum regarding the same issue. My problem lies in capitalizing the first letter of an input. Here is the link to my code on JSFiddle Despite my efforts, I can't seem to get the substr() funct ...

Oops! The system encountered a problem while trying to identify the value `Han` for the property `Script

I'm attempting to extract Chinese characters from a string. According to this particular answer, the following code snippet should work: const nonChinese = /[^\p{Script=Han}]/gimu; const text = "asdP asfasf这些年asfagg 我开源的 几 ...

Display a Tooltip icon alongside a text field label with Vuetify components

Currently, I am utilizing Vuetify's component tooltip and experiencing difficulty implementing it right next to the label. Here is my current approach: <v-tooltip right> <v-icon slot="activator" dark color="primary">info</v-icon> ...

Injecting custom page headers based on the Angular 2 environment

Help! I'm trying to incorporate Google Tag Manager into my Angular2 app, but I'm struggling to figure out how to inject the necessary GTM script with different container IDs for development and production. I have two containers set up in GTM. Is ...

The functionality of d3 transition is currently non-existent

I've encountered an issue with my D3 code. const hexagon = this.hexagonSVG.append('path') .attr('id', 'active') .attr('d', lineGenerator(<any>hexagonData)) .attr('stroke', 'url(#gradi ...

What is the best way to get $q.all to function properly in this scenario?

Having some trouble with my implementation of $q.all in the code snippet below. It seems I may have misunderstood a few concepts as it's not functioning as expected. Would greatly appreciate any guidance or tips. The specific problem lies within $q.a ...

How to store selected checkbox values in an array using AngularJS

Check out this code snippet: <tr ng-repeat="doc in providers"> <td><input type="checkbox" ng-true-value="{{doc.provider.Id}}" ng-false-value="" ng-model="ids"></td> </tr> {{ids}} I am trying to store the values of ...

Website/Application - Live Charting Assistance (MySQL, JavaScript, PHP?)

Currently, I am focused on developing a project that involves creating real-time charts using data from an SQL database. After conducting extensive research on JS charting libraries, I have identified a few excellent options. However, my primary challenge ...

Angular 6: Extracting an array from another

I am dealing with an array of IVisitView objects containing visitDate and clientName information. export interface IVisitView { visitDate?: Moment; clientName?: string; } export class VisitView implements IVisitView { constructor( public visitD ...

What is the best way to utilize class labels?

I've encountered an issue I need help with. There are two lists of options in play here. One list has names all starting with 'M', and the other with 'R'. The task is to select specific options, like 'A' and 'C&apos ...