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

javascript - how to push value to v-model on-click button - Stack Overflow

programmeradmin1浏览0评论

I have a looping data and I want to push/add/update the value to v-model DataJournals.Description when I click the button.

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.

I have a looping data and I want to push/add/update the value to v-model DataJournals.Description when I click the button.

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.

Share Improve this question edited Feb 28, 2019 at 5:02 fiza khan 1,29614 silver badges24 bronze badges asked Feb 28, 2019 at 4:59 JazulyJazuly 1,4145 gold badges22 silver badges49 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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

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

You need to update reference to this.DataJournal to make ponent reactive.

You need to use index to update data and it is better to render a link to inside td element

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./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>

发布评论

评论列表(0)

  1. 暂无评论