检索 v-for 数组中 textarea 的值
Retrieving the value of textarea in a v-for array
考虑这种情况
在 .vue template
<div v-for="(tweet, index) in tweets">
<div class="each_tweet">
<textarea v-on:keyup="typing(index)"
placeholder="What's happening now">{{ tweet.content }}</textarea>
</div>
</div>
在 .vue <script>
export default {
methods: {
typing: function(index) {
console.log(this.tweets[index])//How to get the value of textarea
}
},
data: function () {
return {
tweets: [{id: 0, content: ""},
{id: 1, content: ""}]
}
}
}
我的问题是:
1) 有没有办法将文本区域的值与每个推文对象的内容同步?该值是指textarea的innerText。
2) 有没有办法在"typing"方法中获取textarea的值?
您可以使用 v-model
:
You can use the v-model directive to create two-way data bindings on
form input and textarea elements.
例如:
<div v-for="(tweet, index) in tweets">
<div class="each_tweet">
<textarea v-on:keyup="typing(index)"
v-model="tweet.content"
placeholder="What's happening now"></textarea>
</div>
</div>
考虑这种情况
在 .vue template
<div v-for="(tweet, index) in tweets">
<div class="each_tweet">
<textarea v-on:keyup="typing(index)"
placeholder="What's happening now">{{ tweet.content }}</textarea>
</div>
</div>
在 .vue <script>
export default {
methods: {
typing: function(index) {
console.log(this.tweets[index])//How to get the value of textarea
}
},
data: function () {
return {
tweets: [{id: 0, content: ""},
{id: 1, content: ""}]
}
}
}
我的问题是:
1) 有没有办法将文本区域的值与每个推文对象的内容同步?该值是指textarea的innerText。
2) 有没有办法在"typing"方法中获取textarea的值?
您可以使用 v-model
:
You can use the v-model directive to create two-way data bindings on form input and textarea elements.
例如:
<div v-for="(tweet, index) in tweets">
<div class="each_tweet">
<textarea v-on:keyup="typing(index)"
v-model="tweet.content"
placeholder="What's happening now"></textarea>
</div>
</div>