Javascript函数没有首次获得所有值 [英] Javascript function not getting all values first time

查看:129
本文介绍了Javascript函数没有首次获得所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让用户上传项目列表以过滤搜索结果表格。问题在于并不是所有在过滤器列表中没有值的行都会在第一次被删除,所以为了让程序能够做我想做的事情,我需要多次运行它。

I am trying to let the user upload a list of items to filter a search results table by. The problem is that not all rows that don't have values in the filter list are removed the first time, so to get the program to do what I want I need to run it multiple times.

附加的代码有效,但我想删除'badWorkAround()'函数

The attached code works, but I want to remove the 'badWorkAround()' function

<div align="center">
<script>
  function readList() {
  var origText = document.getElementById("CusipTextArea").value;
  var filterList = origText.split("\n");
  var table = document.getElementById("resultsTable");
  var rows = table.getElementsByTagName("tr"); 
  for(i = 1; i < rows.length; i++)  #first row has the column names so I start at i=1
 { 
   var Found = '0'
   for(j = 0; j < filterList.length; j++)
    {   
        if (rows[i].innerHTML.indexOf(filterList[j]) > 0){
        Found = '1'
        break;
            } 
        } 
    if (Found != '1'){rows[i].remove();}    
     } 
  }
  function badWorkAround(){
        readList();
        readList();
        readList();
        readList();
        readList();
        readList();
        readList();
        readList();
        readList();
  }
</script>
<textarea id=TextArea>Paste in a list to filter by. One per line please.</textarea><BR>
<button type="button" onclick="return badWorkAround();">Filter by List</button>
</div>


推荐答案

NodeLists(像getElementsByTagName一样)是live ,意思是它们随时反映DOM 的当前状态。

NodeLists (like you get with getElementsByTagName) are "live", meaning they reflect the current state of the DOM at any time.

您正在循环TR节点,然后删除一些他们这样做。假设你在循环中的索引为1(第二次循环迭代),并删除该节点 - 所有后续节点向上滑动一个位置,以便以前是具有索引2的节点现在具有索引1,什么是3现在是2等等。但是,由于您还在每个循环中增加循环计数器 i ,所以在下一次迭代中,您访问现在是的节点在索引2(之前的3)处的那个 - 以及之前在位置2 处的节点甚至没有被处理过。

You are looping over the TR nodes, and then removing some of them while doing so. Lets say you are at index 1 in your loop (second loop iteration), and remove that node – all following nodes "slide up" one position, so that what formerly was the node with the index 2 has now index 1, and what was 3 is now 2, etc. But since you are also increasing your loop counter i by one each loop, in your next iteration you access the node that is now the one at index 2 (formerly 3) – and the node that was at position 2 before is not even processed any more.

针对此问题的最简单解决方案:从 rows.length - 1 0 向后循环向后 c $ c>(和减少你的循环计数器变量,那么很明显)。因为如果您在列表的 end 处删除了一个节点,那么它不会影响之前的任何节点的位置。

Easiest solution for this problem: Loop over the nodes backwards, from rows.length - 1 to 0 (and decrease your loop counter variable then, obviously). Because if you remove a node at the end of the list, that does not affect the position of any nodes before it.

这篇关于Javascript函数没有首次获得所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆