单击按钮后,如何在VueJS中清除输入文本?

时间:2018-04-11 07:05:16

标签: javascript function input vue.js innerhtml

我希望输入框中的文字在按下输入按钮后清除,并将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)
        }

    }
})

1 个答案:

答案 0 :(得分:5)

清除你的模特:

addTask: function() {
                var task = this.newTask
                this.todoList.push(task)

                this.newTask = ''
            }