【问题标题】:How can I clear my input text in VueJS after a button is clicked?单击按钮后,如何清除 VueJS 中的输入文本?
【发布时间】: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


【解决方案1】:

清除你的模型:

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

                this.newTask = ''
            }

【讨论】:

  • 您认为 v-model 对象无法按预期工作的原因是什么?表示,不清除值
猜你喜欢
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 2014-02-27
  • 1970-01-01
  • 1970-01-01
  • 2022-10-20
  • 2013-04-20
  • 1970-01-01
相关资源
最近更新 更多