我想从姓名和国家数值中搜索

huangapple go评论58阅读模式
英文:

I want to search from both Name and Country values

问题

我想在“Name”和“Country”值列上使用文本过滤器进行搜索,但现在我只能在一列上进行搜索。我该如何更改tr[i].getElementsByTagName('td')[0]的值以同时过滤这两列?

我会很高兴如果你可以帮助我。

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-html -->
    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">

    <table id="myTable">
      <tr class="header">
        <th style="width:60%;">Name</th>
        <th style="width:40%;">Country</th>
      </tr>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Berglunds snabbkop</td>
        <td>Sweden</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Koniglich Essen</td>
        <td>Germany</td>
      </tr>
    </table>
    <script>
    function myFunction() {
      // 声明变量
      var input, filter, table, tr, td, i, txtValue;
      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) {
          txtValue = td.textContent || td.innerText;
          if (txtValue.toUpperCase().indexOf(filter) > -1) {
            tr[i].style.display = "";
          } else {
            tr[i].style.display = "none";
          }
        }
      }
    }
    </script>

<!-- 结束代码片段 -->
英文:

I want to search on the Name and Country values ​​columns with the text filter, but now I can only search on one column. How can I change the value of tr[i].getElementsByTagName(&#39;td&#39;)[0] to filter on both columns?

I'll be happy if you can help me.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;input type=&quot;text&quot; id=&quot;myInput&quot; onkeyup=&quot;myFunction()&quot; placeholder=&quot;Search for names..&quot;&gt;
&lt;table id=&quot;myTable&quot;&gt;
&lt;tr class=&quot;header&quot;&gt;
&lt;th style=&quot;width:60%;&quot;&gt;Name&lt;/th&gt;
&lt;th style=&quot;width:40%;&quot;&gt;Country&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alfreds Futterkiste&lt;/td&gt;
&lt;td&gt;Germany&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Berglunds snabbkop&lt;/td&gt;
&lt;td&gt;Sweden&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Island Trading&lt;/td&gt;
&lt;td&gt;UK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Koniglich Essen&lt;/td&gt;
&lt;td&gt;Germany&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;script&gt;
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById(&quot;myInput&quot;);
filter = input.value.toUpperCase();
table = document.getElementById(&quot;myTable&quot;);
tr = table.getElementsByTagName(&quot;tr&quot;);
// Loop through all table rows, and hide those who don&#39;t match the search query
for (i = 0; i &lt; tr.length; i++) {
td = tr[i].getElementsByTagName(&quot;td&quot;)[0];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) &gt; -1) {
tr[i].style.display = &quot;&quot;;
} else {
tr[i].style.display = &quot;none&quot;;
}
}
}
}
&lt;/script&gt;

<!-- end snippet -->

答案1

得分: 1

你可以选择两个字段并要求它与任何一个匹配。

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="搜索姓名或国家..">
<table id="myTable">
    <tr class="header">
        <th style="width:60%;">姓名</th>
        <th style="width:40%;">国家</th>
    </tr>
    <tr>
        <td>Alfreds Futterkiste</td>
        <td>德国</td>
    </tr>
    <tr>
        <td>Berglunds snabbkop</td>
        <td>瑞典</td>
    </tr>
    <tr>
        <td>Island Trading</td>
        <td>英国</td>
    </tr>
    <tr>
        <td>Koniglich Essen</td>
        <td>德国</td>
    </tr>
</table>
<script>
    // 声明变量
    var input, filter, table, tr, tdName, tdCountry, i, txtValueName, txtValueCountry;
    input = document.getElementById("myInput");
    table = document.getElementById("myTable");
    tr = table.getElementsByTagName("tr");

    // 初始隐藏所有表格行
    for (i = 0; i < tr.length; i++) {
        if (tr[i].classList.contains('header')) {
            continue;
        }
        tr[i].style.display = "none";
    }

    function myFunction() {
        filter = input.value.toUpperCase();

        // 遍历所有表格行,并隐藏那些不匹配搜索查询的姓名或国家列
        for (i = 0; i < tr.length; i++) {
            tdName = tr[i].getElementsByTagName("td")[0];
            tdCountry = tr[i].getElementsByTagName("td")[1];
            if (tdName && tdCountry) {
                txtValueName = tdName.textContent || tdName.innerText;
                txtValueCountry = tdCountry.textContent || tdCountry.innerText;
                if (txtValueName.toUpperCase().indexOf(filter) > -1 || txtValueCountry.toUpperCase().indexOf(filter) > -1) {
                    tr[i].style.display = "";
                } else {
                    tr[i].style.display = "none";
                }
            }
        }
    }
</script>
英文:

You can select both fields and ask it to match against either.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<table id="myTable">
<tr class="header">
<th style="width:60%;">Name</th>
<th style="width:40%;">Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Germany</td>
</tr>
<tr>
<td>Berglunds snabbkop</td>
<td>Sweden</td>
</tr>
<tr>
<td>Island Trading</td>
<td>UK</td>
</tr>
<tr>
<td>Koniglich Essen</td>
<td>Germany</td>
</tr>
</table>
<script>
// Declare variables
var input, filter, table, tr, tdName, tdCountry, i, txtValueName, txtValueCountry;
input = document.getElementById("myInput");
table = document.getElementById("myTable");
tr = table.getElementsByTagName("tr");

// Hide all table rows initially
for (i = 0; i &lt; tr.length; i++) {
if (tr[i].classList.contains(&#39;header&#39;)) {
continue;
}
tr[i].style.display = &quot;none&quot;;
}
function myFunction() {
filter = input.value.toUpperCase();
// Loop through all table rows, and hide those that don&#39;t match the search query in either the Name or Country column
for (i = 0; i &lt; tr.length; i++) {
tdName = tr[i].getElementsByTagName(&quot;td&quot;)[0];
tdCountry = tr[i].getElementsByTagName(&quot;td&quot;)[1];
if (tdName &amp;&amp; tdCountry) {
txtValueName = tdName.textContent || tdName.innerText;
txtValueCountry = tdCountry.textContent || tdCountry.innerText;
if (txtValueName.toUpperCase().indexOf(filter) &gt; -1 || txtValueCountry.toUpperCase().indexOf(filter) &gt; -1) {
tr[i].style.display = &quot;&quot;;
} else {
tr[i].style.display = &quot;none&quot;;
}
}
}
}

</script>

<!-- end snippet -->

答案2

得分: 1

尝试这个,

<script>
function myFunction() {
  // 声明变量
  var input, filter, table, tr, td, td1, i, txtValue, txtValue1;
  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];
    td1 = tr[i].getElementsByTagName("td")[1];

    if (td) {
      txtValue = td.textContent || td.innerText;
      txtValue1 = td1.textContent || td1.innerText;
      console.log(txtValue1);
      if (txtValue.toUpperCase().indexOf(filter) > -1 || txtValue1.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
</script>

注意:我已经移除了 HTML 编码字符(如 &lt;&quot;)并将其替换为正常的 HTML 标签和引号。

英文:

Try this,

&lt;script&gt;
function myFunction() {
// Declare variables
var input, filter, table, tr, td, i, txtValue,txtValue1;
input = document.getElementById(&quot;myInput&quot;);
filter = input.value.toUpperCase();
table = document.getElementById(&quot;myTable&quot;);
tr = table.getElementsByTagName(&quot;tr&quot;);
// Loop through all table rows, and hide those who don&#39;t match the search query
for (i = 0; i &lt; tr.length; i++) {
td = tr[i].getElementsByTagName(&quot;td&quot;)[0];
td1 = tr[i].getElementsByTagName(&quot;td&quot;)[1];
if (td) {
txtValue =  td.textContent || td.innerText ;
txtValue1 =  td1.textContent || td1.innerText ;
console.log(txtValue1);
if (txtValue.toUpperCase().indexOf(filter) &gt; -1 || txtValue1.toUpperCase().indexOf(filter) &gt; -1) {
tr[i].style.display = &quot;&quot;;
} else {
tr[i].style.display = &quot;none&quot;;
}
}
}
}
&lt;/script&gt;

答案3

得分: 1

我采用了一个更简单的方法。

假设你想搜索 tr 元素的内容。我在文本内容中使用 includes 函数来查找筛选条件。我使用这个结果来切换 tr 上的一个类。

CSS 类将显示设置为 block,同时我还使用 CSS 将 TR 默认设置为 display:none。

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">

<table id="myTable">
  <tr class="header">
    <th style="width:60%;">Name</th>
    <th style="width:40%;">Country</th>
  </tr>
  <tr>
    <td>Alfreds Futterkiste</td>
    <td>Germany</td>
  </tr>
  <tr>
    <td>Berglunds snabbkop</td>
    <td>Sweden</td>
  </tr>
  <tr>
    <td>Island Trading</td>
    <td>UK</td>
  </tr>
  <tr>
    <td>Koniglich Essen</td>
    <td>Germany</td>
  </tr>
</table>
const input = document.getElementById("myInput");
const table = document.getElementById("myTable");
const trs = table.querySelectorAll("tr:not(.header)");

function myFunction() {  
  const filter = input.value.toUpperCase();
  trs.forEach((tr) => {
      tr.classList.toggle("active", (filter != "" && tr.textContent.toUpperCase().includes(filter)))
  });
}
tr:not(.header) {
  display: none;
}

tr.active {
  display: block;
}
英文:

I went with a simpler approach.

Assuming, you want to search the content of the tr itself. I'm looking in the text content for the filter using the includes function. I use the return of that to toggle a class on the tr.

The CSS class sets the display to block, while I also use CSS to set TR to display none by default.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = document.getElementById(&quot;myInput&quot;);
const table = document.getElementById(&quot;myTable&quot;);
const trs = table.querySelectorAll(&quot;tr:not(.header)&quot;);
function myFunction() {  
const filter = input.value.toUpperCase();
trs.forEach((tr) =&gt; {
tr.classList.toggle(&quot;active&quot;,(filter != &quot;&quot; &amp;&amp; tr.textContent.toUpperCase().includes(filter)))
});
}

<!-- language: lang-css -->

tr:not(.header){display:none}
tr.active{display:block}

<!-- language: lang-html -->

&lt;input type=&quot;text&quot; id=&quot;myInput&quot; onkeyup=&quot;myFunction()&quot; placeholder=&quot;Search for names..&quot;&gt;
&lt;table id=&quot;myTable&quot;&gt;
&lt;tr class=&quot;header&quot;&gt;
&lt;th style=&quot;width:60%;&quot;&gt;Name&lt;/th&gt;
&lt;th style=&quot;width:40%;&quot;&gt;Country&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alfreds Futterkiste&lt;/td&gt;
&lt;td&gt;Germany&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Berglunds snabbkop&lt;/td&gt;
&lt;td&gt;Sweden&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Island Trading&lt;/td&gt;
&lt;td&gt;UK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Koniglich Essen&lt;/td&gt;
&lt;td&gt;Germany&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 14:27:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定