W
W
websiller2018-09-22 15:09:48
Vue.js
websiller, 2018-09-22 15:09:48

Submitting a form after changing data in Vue.js?

There is a form:

<form ref="form" method="POST">
  <input type="text" :value="name">
</form>
<a href="#" @click.prevent="submit('Вася')">Инициирует отправку формы</a>

js component:
{
  data{
    name: '',
  },
  methods:{
    submit(name){
      this.name = name;
      // форма отправляется до того, как изменится value инпута
      this.$refs.form.submit();
    }
  }
}

The problem is that the form will submit before the value of the field of this form changes, as a result, the server does not receive the necessary data. It works if you write setTimeout(()=>this.$refs.form.submit(), 0) instead of this.$refs.form.submit() . But what would be the right thing to do in this case? And why does this happen at all? The code with the form and the component is just an example, in a real application everything is more complicated, it is this moment with sending that interests.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-09-22
@websiller

It works if you write setTimeout(()=>this.$refs.form.submit(), 0) instead of this.$refs.form.submit() . But what would be the right thing to do in this case?

because

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question