【发布时间】:2018-09-20 22:55:45
【问题描述】:
我希望在按下输入按钮并将 addTask 函数添加到列表后清除输入框中的文本。我试过 document.getElementById("inp").innerHTML = "" 但它没有用。我该怎么做?
HTML:
<div id="todo">
<h1>To-Do List</h1>
<section>
<input type="input" placeholder="what do you need to do?" v-model="newTask" v-on:keyup.enter="addTask" id="inp">
</section>
<ul>
<li v-for="task in todoList">
<label>{{ task }}</label>
<button type="button" v-on:click="removeTask(task)">X</button>
</li>
</ul>
</div>
VueJS:
var todo = new Vue({
el: 'div#todo',
data: {
newTask:'',
todoList: []
},
methods: {
addTask: function() {
var task = this.newTask
this.todoList.push(task)
},
removeTask: function(task) {
var index = this.todoList.indexOf(task)
this.todoList.splice(index, 1)
}
}
})
【问题讨论】:
-
@Codeer 这不是在 Vue 等框架中更改值的正确方法。
标签: javascript function input vue.js innerhtml