gpt4 book ai didi

javascript - JS 过滤器允许带有 AND 且不带破折号的数字

转载 作者:行者123 更新时间:2023-11-28 04:17:51 25 4
gpt4 key购买 nike

我需要过滤一个在打字时实时过滤的表格。它将使用电话号码,但需要能够接受带破折号和不带破折号的数字。搜索该号码时,905-000-7777 和 9050007777 都应该可以接受。

这是我迄今为止所拥有的,我已经尝试了几个小时:

<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}

#myTable th, #myTable td {
text-align: left;
padding: 12px;
}

#myTable tr {
border-bottom: 1px solid #ddd;
}

#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
</style>
</head>
<body>

<h2>Number search</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<table id="myTable">
<tr class="header">
<th style="width:60%;">Number</th>
</tr>
<tr>
<td>905-000-7777</td>
</tr>
<tr>
<td>905-282-9992</td>
</tr>
<tr>
<td>9050107777</td>
</tr>

</table>

<script>
function myFunction() {
var input, filter, table, tr, td, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];
if (td) {
if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
</script>

</body>
</html>

目前我可以让它过滤号码,但在电话号码中,您必须完美匹配表变量。我希望它接受这个或那个。我对 js 没有太多经验,事实证明这比我想象的要困难。

最佳答案

正如其他人在评论中已经解释的那样,您可以在比较之前从输入和单元格内容中删除 - 连字符。请参阅下面的工作 fiddle :

cleanedFilter = filter.replace("-","");

cellContent = td.innerHTML.toUpperCase().replace("-","");

function myFunction() {
var input, filter, table, tr, td, i, cleanedFilter;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");

cleanedFilter = filter.replace("-","");

for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td")[0];

if (td) {
cellContent = td.innerHTML.toUpperCase().replace("-","");

if (cellContent.indexOf(cleanedFilter) > -1) {
tr[i].style.display = "";
} else {
tr[i].style.display = "none";
}
}
}
}
* {
box-sizing: border-box;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 10px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myTable {
border-collapse: collapse;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
}

#myTable th, #myTable td {
text-align: left;
padding: 12px;
}

#myTable tr {
border-bottom: 1px solid #ddd;
}

#myTable tr.header, #myTable tr:hover {
background-color: #f1f1f1;
}
<h2>Number search</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<table id="myTable">
<tr class="header">
<th style="width:60%;">Number</th>
</tr>
<tr>
<td>905-000-7777</td>
</tr>
<tr>
<td>905-282-9992</td>
</tr>
<tr>
<td>9050107777</td>
</tr>

</table>

关于javascript - JS 过滤器允许带有 AND 且不带破折号的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45686037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com