简体   繁体   English

检测输入的空白​​字段

[英]Detecting an empty field of an input

I create a search bar. 我创建一个搜索栏。 When the user presses the letter 'A' I want to display the users whose first name starts with the letter 'A'. 当用户按下字母“ A”时,我要显示其名字以字母“ A”开头的用户。 This works, but my problem is when the search bar is empty. 这可行,但是我的问题是搜索栏为空。 When it is empty, my program displays all the users, and I want it to show none. 当它为空时,我的程序将显示所有用户,并且我希望它不显示任何用户。

Here is my program to obtain the value entered by the user : 这是我的程序,用于获取用户输入的值:

searchUser(): void {
  let str;
  let element = document.getElementById('chat-window-input');

  if(element !== null) {
    str = (element as HTMLInputElement).value;
  }
  else {
    str = null;
  }
}

Do you know how to detect an empty field ? 您知道如何检测空字段吗? I tested this but it does not change anything : 我对此进行了测试,但没有任何改变:

if(str == null) {
    this.searchArrayThreads = [];
  }

Hi you can try like this, 嗨,您可以尝试这样,

 if(str == null || (!str) || str=='') {
  this.searchArrayThreads = [];
}

In your question it seems you have one array of data. 在您的问题中,您似乎拥有一组数据。 so keep a tempDataSet 所以保持一个tempDataSet

this.tempDataSet = []; then while filtering you can directly assign values. 然后在过滤时您可以直接分配值。

this.str = '';
searchUser(): void {
  let element = document.querySelector('#chat-window-input');
  if(element && element.value) {
    return this.str = element.value;
  }
}

I think this will help you 我想这对你有帮助

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

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