Jquery 如何在keyup上显示div搜索未找到记录消息

Jquery 如何在keyup上显示div搜索未找到记录消息,jquery,Jquery,当用户输入时,我正在搜索div。 如果没有记录,我应该显示一条消息“没有记录” 如果我在搜索框内按“H”,则最初显示的消息为“无记录” 这是我的js代码 $('#searchequip').keyup(function() { var val = $.trim(this.value).toUpperCase(); $(".mt-checkbox").each(function() { var parent = $(t

当用户输入时,我正在搜索div。 如果没有记录,我应该显示一条消息“没有记录”

如果我在搜索框内按“H”,则最初显示的消息为“无记录”

这是我的js代码

$('#searchequip').keyup(function()
{
        var val = $.trim(this.value).toUpperCase();
        $(".mt-checkbox").each(function()
        {
                var parent = $(this).closest('li'),
                        length = $(this).text().length > 0;
                if (length && $(this).text().search(new RegExp(val, "i")) < 0)
                {
                        parent.fadeOut("slow");
                        console.log('Nothing Found ');
                        $("#errmsg").html("No Results Found").show().fadeOut("slow");
                }
                else
                {
                        parent.show();
                }
        });
})
$('searchEquipment').keyup(函数()
{
var val=$.trim(this.value).toUpperCase();
$(“.mt复选框”)。每个(函数()
{
var parent=$(this).closest('li'),
长度=$(this).text().length>0;
if(length&&$(this.text().search(new RegExp(val,“i”)))<0)
{
母公司。淡出(“缓慢”);
console.log('Nothing Found');
$(“#errmsg”).html(“未找到结果”).show().fadeOut(“缓慢”);
}
其他的
{
parent.show();
}
});
})
这是我的小提琴


我建议您定义一个标志变量来确定是否找到该单词,然后在
每个功能之后显示“未找到结果”文本:

var val = $.trim(this.value).toUpperCase();
var found = false;
$(".mt-checkbox").each(function() {
    var parent = $(this).closest('li'),
        length = $(this).text().length > 0;
    if (length && $(this).text().search(new RegExp(val, "i")) < 0) {
        parent.fadeOut("slow");
        console.log('Nothing Found ');
    } else {
        found = true;
        parent.show();
    }
});

if (!found) $("#errmsg").html("No Results Found").show().fadeOut("slow");
var val=$.trim(this.value).toUpperCase();
var=false;
$(“.mt复选框”)。每个(函数(){
var parent=$(this).closest('li'),
长度=$(this).text().length>0;
if(length&&$(this.text().search(new RegExp(val,“i”)))<0){
母公司。淡出(“缓慢”);
console.log('Nothing Found');
}否则{
发现=真;
parent.show();
}
});
如果(!found)$(“#errmsg”).html(“未找到结果”).show().fadeOut(“慢”);

我建议您定义一个标志变量来确定是否找到该单词,然后在
每个功能之后显示“未找到结果”文本:

var val = $.trim(this.value).toUpperCase();
var found = false;
$(".mt-checkbox").each(function() {
    var parent = $(this).closest('li'),
        length = $(this).text().length > 0;
    if (length && $(this).text().search(new RegExp(val, "i")) < 0) {
        parent.fadeOut("slow");
        console.log('Nothing Found ');
    } else {
        found = true;
        parent.show();
    }
});

if (!found) $("#errmsg").html("No Results Found").show().fadeOut("slow");
var val=$.trim(this.value).toUpperCase();
var=false;
$(“.mt复选框”)。每个(函数(){
var parent=$(this).closest('li'),
长度=$(this).text().length>0;
if(length&&$(this.text().search(new RegExp(val,“i”)))<0){
母公司。淡出(“缓慢”);
console.log('Nothing Found');
}否则{
发现=真;
parent.show();
}
});
如果(!found)$(“#errmsg”).html(“未找到结果”).show().fadeOut(“慢”);
这是代码;

$('searchEquipment').keyup(函数(){
var val=$.trim(this.value).toUpperCase();
var noElem=真;
$('.mt复选框')。每个(函数(){
var parent=$(this).closest('li'),
长度=$(this).text().length>0;
if(length&&$(this.text().search(新的RegExp(val,'i')))<0)
{
母公司。淡出(“缓慢”);
}否则{
noElem=假;
parent.show();
}
});
if(noElem)
$('#errmsg').html('未找到结果').show().fadeOut('慢');
})
这是代码;

$('searchEquipment').keyup(函数(){
var val=$.trim(this.value).toUpperCase();
var noElem=真;
$('.mt复选框')。每个(函数(){
var parent=$(this).closest('li'),
长度=$(this).text().length>0;
if(length&&$(this.text().search(新的RegExp(val,'i')))<0)
{
母公司。淡出(“缓慢”);
}否则{
noElem=假;
parent.show();
}
});
if(noElem)
$('#errmsg').html('未找到结果').show().fadeOut('慢');
})

问题在于,您正在更新
每个
循环中的
#errmsg
范围,而实际上您应该在搜索结束时更新它。查看更新的代码

$('searchEquipment').keyup(函数()
{
var val=$.trim(this.value).toUpperCase();
变量长度=假;
var globalfound=-1;
$(“.mt复选框”)。每个(函数()
{
var parent=$(this).closest('li');
长度=$(this).text().length>0;
localfound=$(this.text().search(newregexp(val,“i”));
如果(globalfound<0){globalfound=localfound;}
if(长度和本地查找<0)
{
母公司。淡出(“缓慢”);
}否则{
parent.show();
}
});
if(长度和全局声音<0){
console.log('Nothing Found');
$(“#errmsg”).html(“未找到结果”).show().fadeOut(“缓慢”);
}否则{
$(“#errmsg”).html(“未找到结果”).hide()
}
})
#errmsg
{
颜色:红色;
}

  • 希拉里·克林顿
  • 唐纳德·特朗普

问题在于,您正在更新
每个
循环中的
#errmsg
范围,而实际上您应该在搜索结束时更新它。查看更新的代码

$('searchEquipment').keyup(函数()
{
var val=$.trim(this.value).toUpperCase();
变量长度=假;
var globalfound=-1;
$(“.mt复选框”)。每个(函数()
{
var parent=$(this).closest('li');
长度=$(this).text().length>0;
localfound=$(this.text().search(newregexp(val,“i”));
如果(globalfound<0){globalfound=localfound;}
if(长度和本地查找<0)
{
母公司。淡出(“缓慢”);
}否则{
parent.show();
}
});
if(长度和全局声音<0){
console.log('Nothing Found');