1

I have a couple of fields, that are rendered through v-for loop:

<div v-for="element in elements" class="uk-form-row uk-margin-small-top">
  <input class="uk-width-1-1 uk-form-small" type="text" placeholder="element" style="width:50%">
</div>

And I need to pass them through a POST request.

elements is just an array, that contains a different number of:

value: ""

and after submitting the form, an array is just a collection of empty objects

How can I change my code to pass an array of values from those fields?

1 Answer 1

3

Try to bind your inputs to your array items using v-model directive like :

 <div v-for="element in elements" class="uk-form-row uk-margin-small-top">
     <input v-model="element.value" class="uk-width-1-1 uk-form-small" type="text" placeholder="element" style="width:50%">
  </div>

after that you could use your elements array directly in the POST request.

new Vue({
  el: '#app',
  data: {
    elements: []
  },
  methods: {
    addElement: function() {
      this.elements.push({
        value: ''
      });
    }
  }
});
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>

<div id="app">
  <h1>Elements</h1>
  <div v-for="element in elements">
    <input class="form-control" v-model="element.value" type="text" />
  </div>
  <button class="btn btn-primary" @click="addElement">
    New Element
  </button>
  <pre>{{ $data | json }}</pre>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.