简体   繁体   English

使用 Trim 方法如何在 vuejs 中删除空格/输入?

[英]Using Trim method how to remove space/enter in vuejs?

 processSearch() { this.search.trim().length>0; // this.searchHistory.push(this.search); this.search = ''; },

 preventLeadingSpace(e) { // only prevent the keypress if the value is blank if (.e.target.value) e;preventDefault(), // otherwise, if the leading character is a space. remove all leading white-space else if (e.target.value[0]==' ') e.target.value = e.target.value,replace(/^\s*/; ""), },
 <input id="SearchText" type="text" v-model="search" @keydown.enter="enter" @click="onClick" @keyup.enter="processSearch" @input="change" @keyup="inputChanged" @keydown.down="onArrow" @keydown.up="onArrow" @keydown.space="preventLeadingSpace" /> <ul class="list-inline" > <li class="list-inline-item list-group-item-primary" v-for="(item, index) in searchHistory.slice(-5).reverse().map((s) => s.trim())":key="index" @click="selectPreviousSearch(index)" > {{ item }} </li> </ul>

 this.search.trim().length>0 // tried using this to stop enter, But doesn't work.

I am trying to do the search functionality,If user enter anything in the search, result will be displayed in li.我正在尝试执行搜索功能,如果用户在搜索中输入任何内容,结果将显示在 li 中。 Now the issue is, even without entering any data simply if i click on enter key from the keyboard, then also it is showing as result in li.现在的问题是,即使我没有输入任何数据,只要我从键盘上单击输入键,它也会显示为 li。

Second issue is, If is click on input, dropdown is displaying.第二个问题是,如果单击输入,则会显示下拉菜单。 But when i click outside of input dropdown is not closing.但是当我在输入下拉列表之外单击时不会关闭。

To remove the white spaces, or to trim the input, Vue come up with such functionality.为了去除空格,或者修剪输入, Vue提供了这样的功能。 You can use v-model.trim try this:您可以使用v-model.trim试试这个:

<form @submit.prevent="processSearch">
<input
          id="SearchText"
          type="text"
          v-model.trim="search"
          @keydown.enter="enter"
          @click="onClick"
          @input="change"
          @keyup="inputChanged"
          @keydown.down="onArrow"
          @keydown.up="onArrow"
          @keydown.space="preventLeadingSpace"
         
        />
</form>

Then your processSearch function:然后你的进程搜索processSearch

processSearch() {
       if(this.search){
            this.searchHistory.push(this.search); this.search = '';
         } 
 } 

Let me help you on how to avoid duplicates on your <li> items让我帮助您了解如何避免<li>项上的重复项

.... 
data(){
    return{
    items: [], 
    search: "" 
} 
}, methods:{
processSearch() {
       if(this.search){
            this.searchHistory.push(this.search); this.search = '';
           if(this.items.indexOf(this.search) === -1){
        this.items.push(this.search)
        } 
         } 
 } 
}
.... 

Now on your lists现在在你的名单上

<ul class="list-inline"  >
    
      <li
        class="list-inline-item list-group-item-primary"
        v-for="(item, index) in items
          .slice(-5)
          .reverse()
          .map((s) => s.trim())"
        :key="index"
       ..... 
      >
        {{ item }}
      </li>

    </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM