最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to set value to input field in vuejs - Stack Overflow

programmeradmin0浏览0评论
<div class="col-md-8">
    <va-input label="Address 1"
        v-model="Address1"
        id="address"
        class="inp">
    </va-input>
</div>

below i am calling api to get data. after getting i need to set value to above input field.

document.getElementById("address").value =res.data[0].address1,

but the above code is not working.

<div class="col-md-8">
    <va-input label="Address 1"
        v-model="Address1"
        id="address"
        class="inp">
    </va-input>
</div>

below i am calling api to get data. after getting i need to set value to above input field.

document.getElementById("address").value =res.data[0].address1,

but the above code is not working.

Share Improve this question edited Dec 19, 2019 at 8:25 Jesper 3,8442 gold badges18 silver badges24 bronze badges asked Dec 19, 2019 at 7:58 pavan kppavan kp 691 gold badge4 silver badges15 bronze badges 3
  • 2 this is not an vuejs way. – Devsi Odedra Commented Dec 19, 2019 at 8:00
  • Change the model value Address1 and not by directly manipulating dom. Ex: this.Address1 = res.data[0].address1 – ambianBeing Commented Dec 19, 2019 at 8:03
  • Pavan, do you have the Address1 variabled in the data field? – Jesper Commented Dec 19, 2019 at 8:26
Add a ment  | 

2 Answers 2

Reset to default 1

Try using the ref property, see here: https://v2.vuejs/v2/api/#ref Basically would look something like:


    <va-input label="Address 1"
              v-model="Address1"
              id="address"
              class="inp"
              ref="inputRef"
    >
         </va-input>
    ...
    this.$refs.inputRef.$el.value = ...

You may need to dig a little into the structure but from the $el you can access the element.

You need to add this data to the "data" of the ponent, just create :

`data() {
    return {
        Address1: ''
    }
}`

and at the created() or anywhere you want to assign this value : this.Adress1 = res.data[0].address1

Basically this is the vue way of doing these kind of things,and using the v-model properly.

发布评论

评论列表(0)

  1. 暂无评论