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

javascript - Vue JS: ResetClear value from input - Stack Overflow

programmeradmin6浏览0评论

I am building a search feature for my Nuxt Js front end and I need the input value to clear whenever the user has pressed enter to search. This is my current method and markup.

Js

methods: {
        searchTag: function(event) {
            var newtag = event.target.value;
            this.tags.push({name: newtag});
        }
    }

Markup

<input type="text" placeholder="Search for tags..." v-on:keyup.enter="searchTag">

I tried adding event.target.reset(); but it didn't work, seems like such an easy thing but cannot find an answer anywhere, also want to stay away from using Jquery / plain JS as everything else is done without them and dont want to add it to the application for the sake of a tiny feature.

Thanks, Jamie

I am building a search feature for my Nuxt Js front end and I need the input value to clear whenever the user has pressed enter to search. This is my current method and markup.

Js

methods: {
        searchTag: function(event) {
            var newtag = event.target.value;
            this.tags.push({name: newtag});
        }
    }

Markup

<input type="text" placeholder="Search for tags..." v-on:keyup.enter="searchTag">

I tried adding event.target.reset(); but it didn't work, seems like such an easy thing but cannot find an answer anywhere, also want to stay away from using Jquery / plain JS as everything else is done without them and dont want to add it to the application for the sake of a tiny feature.

Thanks, Jamie

Share asked Jul 12, 2019 at 17:17 user3479267user3479267 23210 silver badges35 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Found an answer:

Adding a v-model to it and then setting that have a empty string seems to do the trick.

<input type="text" placeholder="Search for tags..." v-on:keyup.enter="searchTag" v-model="searchText">


export default {
    data() {
        return {
            tags: [
            ],
            searchText: ""
        }
    },
    methods: {
        searchTag: function(event) {
            var newtag = event.target.value;
            this.tags.push({name: newtag});
            this.searchText = "";
        }
    }
}

Note that the searchText is set to "" at the end of the search function.

Just bind the input value to a variable and then clear it on the event like this :

data{
  input : ''
},
methods: {
   searchTag: function() {
       this.input = '';
   }
}
<input type="text" placeholder="Search for tags..."  v-on:keyup.enter="searchTag" v-model="input">

Vue is plain js. Of course you can use it.

发布评论

评论列表(0)

  1. 暂无评论