英文:
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 = ($("#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);
});
<!-- end snippet -->
答案1
得分: 2
检查条件时,请在 if
语句中明确设置一个变量,以指示是否找到了匹配。
const userSearch = $("#userId").val().toLowerCase();
let foundMatch = false;
$('.table tbody tr').find("dl dd").each(function() {
if (this.innerText.toLowerCase().includes(userSearch)) {
$(this).show();
foundMatch = true;
} else {
$(this).hide();
}
});
if (!foundMatch) {
// 显示消息
}
在设置 userSearch
时,不需要使用 || ''''
。.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 = $("#userId").val().toLowerCase();
let foundMatch = false;
$('.table tbody tr').find("dl dd").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 || ''
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 = ($("#userId").val() || '').toLowerCase();
if(!($('.table tbody tr').find("dl dd").length > 0)) {
alert('no dd elements');
} 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);
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论