我有以下代码,请提供一些指导。我需要#no-results元素仅在未找到结果时出现。当前它显示找到IF结果。
$('#no-results').hide();
$('#video-search-text').keyup(function () {
$('.video-search').hide();
$('#no-results').css("display", "none");
var txt = $('#video-search-text').val();
$('.video-search').each(function () {
if ($(this).text().toUpperCase().indexOf(txt.toUpperCase()) != -1) {
$(this).show();
}
else {
$('#no-results').css("display", "block");
}
});
});
为了方便起见,我创建了以下JSFiddle。谢谢
最佳答案
从每个循环的内删除没有结果的检查,并将其放置在搜索完成之后。喜欢
$('#no-results').hide();
$('#video-search-text').keyup(function() {
$('.video-search').hide();
$('#no-results').css("display", "none");
var txt = $('#video-search-text').val();
var resultCount = 0;
$('.video-search').each(function() {
if ($(this).text().toUpperCase().indexOf(txt.toUpperCase()) != -1) {
$(this).show();
resultCount++;
}
});
if (resultCount == 0) {
$('#no-results').css("display", "block");
}
});