Jquery在toggle为false时显示一条消息。

huangapple go评论65阅读模式
英文:

Jquery display a message when toggle is false

问题

I am using jQuery toggle to show/hide some items and need to display a message when the item is not found. My code below does the toggle hide but I want to show a message when there is nothing to toggle.

const userSearch = ($("#userId").val() || '').toLowerCase();

$('.table tbody tr').find("dl dd").each(function() {
  const userfilter = $(this);
  const userNames = userfilter[0].innerText.toLowerCase();
  const index = userNames.substring(userNames.indexOf(userSearch));
  $(this).toggle(index === userSearch);
});
英文:

I am using jquery toggle to show/hide some items and need to display a message when the item is not found. My code below does the toggle hide but I want to show a message when there is nothing to toggle.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const userSearch = ($(&quot;#userId&quot;).val() || &#39;&#39;).toLowerCase();

$(&#39;.table tbody tr&#39;).find(&quot;dl dd&quot;).each(function() {
  const userfilter = $(this);
  const userNames = userfilter[0].innerText.toLowerCase();
  const index = userNames.substring(userNames.indexOf(userSearch));
  $(this).toggle(index === userSearch);
});

<!-- end snippet -->

答案1

得分: 2

检查条件时,请在 if 语句中明确设置一个变量,以指示是否找到了匹配。

const userSearch = $(&quot;#userId&quot;).val().toLowerCase();

let foundMatch = false;
$(&#39;.table tbody tr&#39;).find(&quot;dl dd&quot;).each(function() {
  if (this.innerText.toLowerCase().includes(userSearch)) {
    $(this).show();
    foundMatch = true;
  } else {
    $(this).hide();
  }
});

if (!foundMatch) {
  // 显示消息
}

在设置 userSearch 时,不需要使用 || '&#39;&#39;'.val() 总是返回一个字符串(除非选择器没有匹配任何内容,这不应该发生)。

英文:

Check the condition explicitly in an if statement, so you can set a variable to indicate whether you found a match.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const userSearch = $(&quot;#userId&quot;).val().toLowerCase();

let foundMatch = false;
$(&#39;.table tbody tr&#39;).find(&quot;dl dd&quot;).each(function() {
  if (this.innerText.toLowerCase().includes(userSearch)) {
    $(this).show();
    foundMatch = true;
  } else {
    $(this).hide();
  }
});

if (!foundMatch) {
  // display message
}

<!-- end snippet -->

There's no need for || &#39;&#39; when setting userSearch. .val() always returns a string (unless the selector doesn't match anything, which shouldn't happen).

答案2

得分: 1

I think you want to just check the length of the items and if it is not greater than zero, show a message.

尝试这样做:

const userSearch = $("#userId").val().toLowerCase() || '';
if(!$('.table tbody tr').find("dl dd").length > 0) {
  alert('没有 dd 元素');
} else {
  $('.table tbody tr').find("dl dd").each(function() {
    const userfilter = $(this);
    const userNames = userfilter[0].innerText.toLowerCase();
    const index = userNames.substring(userNames.indexOf(userSearch));
    $(this).toggle(index === userSearch);
  });
}
英文:

I think you want to just check the length of the items and if it is not greater than zero, show a message.

Try this

const userSearch = ($(&quot;#userId&quot;).val() || &#39;&#39;).toLowerCase();
if(!($(&#39;.table tbody tr&#39;).find(&quot;dl dd&quot;).length &gt; 0)) {
  alert(&#39;no dd elements&#39;);
} else {
  $(&#39;.table tbody tr&#39;).find(&quot;dl dd&quot;).each(function() {
    const userfilter = $(this);
    const userNames = userfilter[0].innerText.toLowerCase();
    const index = userNames.substring(userNames.indexOf(userSearch));
    $(this).toggle(index === userSearch);
  });
}

huangapple
  • 本文由 发表于 2023年5月25日 03:04:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定