Javascript JS Jquery从搜索/筛选输入中删除连字符(仅限数字)
我有一个搜索输入字段,其中包含一些连接的javascript,在我的页面上用作过滤器。它基本上接受输入并在h3标记中搜索名称和描述,但也会搜索表(特别是tbody->tr),如果一行不包含匹配项,它会将其隐藏 我遇到了一个关于表体/行内容的问题。每行都有一个数字,通常是6位数字,4后面有连字符,但并不总是(1234-56、9876-54等) 问题是:如果您使用连字符搜索数字,它会正确过滤并仅显示该数字,但如果您只键入不带连字符的6位数字,它会隐藏所有内容,因为它在搜索精确字符串时从技术上找不到匹配项 我已经找到了一些这样做的方法,但是它们只适用于filter变量,但是我需要一些帮助,即使这只是一个小的解决方法。基本上,在tbody/tr中查找匹配项时,我需要它忽略连字符。因此,无论我键入123456还是1234-56,它都将仅显示具有1234-56的匹配行。我希望这是有道理的 Javascript:Javascript JS Jquery从搜索/筛选输入中删除连字符(仅限数字),javascript,jquery,Javascript,Jquery,我有一个搜索输入字段,其中包含一些连接的javascript,在我的页面上用作过滤器。它基本上接受输入并在h3标记中搜索名称和描述,但也会搜索表(特别是tbody->tr),如果一行不包含匹配项,它会将其隐藏 我遇到了一个关于表体/行内容的问题。每行都有一个数字,通常是6位数字,4后面有连字符,但并不总是(1234-56、9876-54等) 问题是:如果您使用连字符搜索数字,它会正确过滤并仅显示该数字,但如果您只键入不带连字符的6位数字,它会隐藏所有内容,因为它在搜索精确字符串时从技术上找不到匹
<script type = "text/javascript">
$(document).ready(function(){
$("#srch-term").keyup(function(){
//For entered search values
// Retrieve the input field text and reset the count to zero
var filter = $(this).val(), count = 0;
var search_regex = new RegExp(filter, "i");
// Loop through the main container as well as the table body and row that contains the match
$(".group-container").each(function(){
//check if filter matches the group name or description
var group_name = $(this).children('h3').text()
var group_description = $(this).children('.uk-text-muted').text()
if(group_name.search(search_regex)>=0 || group_description.search(search_regex)>=0){ // filter matches
$(this).show() // show group
$(this).find("tbody tr").show() // and all children
return // skip tr filtering
}
var no_matches = true
$(this).find("tbody tr").each(function(){
// If the list item does not contain the text phrase fade it out
if ($(this).text().replace('Available','').search(search_regex) < 0) {
$(this).hide();
// Show the list item if the phrase matches and increase the count by 1
} else {
$(this).show();
count++;
no_matches = false
}
});
if(no_matches){ // if no tr matched the search either, hide whole group
$(this).hide();
}
});
});
});
</script>
$(文档).ready(函数(){
$(“#srch term”).keyup(函数(){
//用于输入的搜索值
//检索输入字段文本并将计数重置为零
var filter=$(this).val(),count=0;
var search_regex=新RegExp(过滤器,“i”);
//循环遍历主容器以及包含匹配项的表体和行
$(“.group容器”).each(函数(){
//检查筛选器是否与组名称或描述匹配
var group_name=$(this).children('h3').text()
var group_description=$(this).children('.uk text muted').text()
如果(group_name.search(search_regex)>=0 | | group_description.search(search_regex)>=0){//过滤器匹配
$(this).show()//显示组
$(this.find(“tbody tr”).show()//和所有子项
返回//跳过tr筛选
}
var no_matches=true
$(this).find(“tbody tr”).each(function(){
//如果列表项不包含文本短语,请将其淡出
if($(this).text().replace('Available','').search(search_regex)<0){
$(this.hide();
//如果短语匹配,则显示列表项并将计数增加1
}否则{
$(this.show();
计数++;
无匹配项=false
}
});
如果(no_matches){//如果没有tr匹配搜索,则隐藏整个组
$(this.hide();
}
});
});
});
如果用连字符搜索数字,它会正确过滤并
仅显示该数字,但如果您只键入6位数字,而没有
连字符它隐藏了所有东西,因为它在技术上找不到匹配项
因为它搜索精确的字符串
如果您只想进行基于数字的搜索,则在使用进行比较时从网格文本中删除非数字字符
.replace(/\D/g, "");
成功了
var group_name = $(this).children('h3').text().replace(/\D/g, "");
var group_description = $(this).children('.uk-text-muted').text().replace(/\D/g, "");
后来
if ($(this).text().replace(/\D/g,'').search(search_regex) < 0) {
$(this).hide();
}
if($(this).text().replace(/\D/g.).search(search\u regex)<0){
$(this.hide();
}
我可能会感到困惑,但我不确定这是否就是我要去的地方。用户也可以按沙发、loveseat、椅子等进行搜索,但如果他们按项目编号进行搜索,那么他们将搜索123456或1234-56,因此我仍然需要现有的基于文本的搜索,只是如果他们输入数字,则会拉出连字符,您想根据用户要搜索的内容为搜索准备不同的数据吗?在执行我上面给出的特定replace
之前,您可以检查输入是否都是数字。