简体   繁体   English

如何将值推入v-model单击按钮

[英]how to push value to v-model on-click button

I have a looping data and I want to push/add/update the value to v-model DataJournals.Description when I click the button. 我有一个循环数据,当我单击按钮时,我想将值推送/添加/更新为v-model DataJournals.Description

Here is what I tried. 这是我尝试过的。

template 模板

<tr v-for="(DataJournals, index) in DataJournal" :key="DataJournals.value">
    <td>
        <textarea class="form-control" id="termsofdelivery" v-model="DataJournals.Description" rows="3" v-bind:disabled="DataJournals.SignSave == 1 ? true : false"></textarea>
    </td>
    <a href="javascript:;" v-on:click="greet(DataJournals.id, index)" class="btn btn-sm btn-primary">Speech</a>
</tr>

My method 我的方法

greet: function (event, id, index) {
    this.DataJournal.Description = 'asddsaasddsaasddsa'
}

When I check console.log its working but not push to v-model. 当我检查console.log它的工作但不推送到v模型。

It should be 它应该是

greet: function (event, id, index) {
    this.DataJournal[index].Description = 'asddsaasddsaasddsa'
    this.DataJournal = JSON.parse(JSON.stringify(this.DataJournal))
}

In your html code 在您的html代码中

v-on:click="greet($event, DataJournals.id, index)"

You need to update reference to this.DataJournal to make component reactive. 您需要更新对this.DataJournal引用以使组件具有反应性。

You need to use index to update data and it is better to render a link to inside td element 您需要使用index来更新数据,最好是在td元素内部呈现链接

 new Vue({ el: "#app", data: { DataJournal: [ {value:1, SignSave:1,id:1,Description:""}, {value:2, SignSave:2,id:2,Description:""}, {value:3, SignSave:3,id:3,Description:""}, ] }, methods: { greet: function(id,index) { this.DataJournal[index].Description = "test"; } } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <table> <tbody> <tr v-for="(DataJournals, index) in DataJournal" :key="DataJournals.value"> <td> <textarea class="form-control" id="termsofdelivery" v-model="DataJournals.Description" rows="3" v-bind:disabled="DataJournals.SignSave == 1 ? true : false"></textarea> <a href="javascript:;" v-on:click="greet(DataJournals.id, index)" class="btn btn-sm btn-primary">Speech</a> </td> </tr> </tbody> </table> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM