jQuery:遍历表的所有可见行

发布于 2024-08-09 19:18:12 字数 1108 浏览 9 评论 0原文

我需要编写一些代码来循环遍历表的所有行,然后检查每行中的隐藏输入以查看它是否包含某个值,如果包含,则设置一些值并隐藏该行。 这可能很笨拙,但它有效: (变量“zeile”包含第一行,在循环之前正确初始化):

while (goon) {
  var hf = zeile.attr('id');
  if (hf) {
      hf = document.getElementById(hf.substr(0,hf.length-2) + 'hfMat');
      if (hf.value == mid) {
          found=true;
          goon=false;
          zeile.hide();
     } else {
         cnt +=1 ;
         if (cnt<10) {
               alert('not found, cnt=' + cnt);
            } else {
               goon=false;
         }
       }
     }

     if (goon) {
        zeile = zeile.next('tr');
        goon=zeile.length>0;
     }
}    

好的,首先,带有 document.getElementById(hf...) 的第 4 行看起来像承认失败,它可能是 - 但 $("#"+hf).val() 只是不会这样做,返回未定义:(

正如我所说,这段代码工作正常。但后来我遇到了一个情况两行在 hfMat 字段中包含相同的值,然后我的脚本将再次隐藏第一行,而我认为它应该继续到第二行,并改进了语句以确定要处理的下一行。 (倒数第四行),所以我从 zeile = zeile.next('tr'); 更改为 zeile = zeile.next('tr:visible'); 然而,对于 jQuery 1.3.2,这不起作用,我得到了未定义的结果。我做错了什么?

当然,必须有一种更好的方法来完成此选择,而无需如此混乱的循环,只需使用 jQuery 的一些更高级的机制,但我担心这些超出了我的能力范围。但我很高兴看到一些想法;)

I need to write some code that would loop though all rows of a table, then check a hidden input in each row to see if it contains a certain value and if so, set some values and hide the row.
It might be clumsy, but it worked:
(variable "zeile" contains the first row, is initialized correctly before the loop):

while (goon) {
  var hf = zeile.attr('id');
  if (hf) {
      hf = document.getElementById(hf.substr(0,hf.length-2) + 'hfMat');
      if (hf.value == mid) {
          found=true;
          goon=false;
          zeile.hide();
     } else {
         cnt +=1 ;
         if (cnt<10) {
               alert('not found, cnt=' + cnt);
            } else {
               goon=false;
         }
       }
     }

     if (goon) {
        zeile = zeile.next('tr');
        goon=zeile.length>0;
     }
}    

Ok, first of all, line 4 with the document.getElementById(hf...) looks like admitting defeat and it probably is - but $("#"+hf).val() just wouldn't do it, returned undefined :(

As I said, this code works fine. BUT then I had a case where two rows contained the same value in the hfMat-field, and my script would then hide the first row again, whereas it should have continued to the second. "Easy", I thought, and improved the statement to determine the next row to process (4th line from the end), so I changed from zeile = zeile.next('tr'); to zeile = zeile.next('tr:visible');
However, with jQuery 1.3.2, this doesn't work and I get undefined. What am I doing wrong?

And surely, there must be a much better way to get this selection done without such messy looping, by simply using some of jQuery's more advanced mechanisms, but I'm afraid these are beyond me atm. But I'd be happy to see some ideas ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2024-08-16 19:18:12

当您只是隐藏一个元素时,它不会从文档流中取出。因此,您的 DOM 查询将继续找到它。

在没有看到你的实际 HTML 的情况下,我只能推测你想要完成的事情可以通过以下 jQuery 循环以更简单的方式完成:

$("tr input[value=" + mid + "]").each(function () {
    this.hide();
});

When you simply hide an element, it is not taken out of the document flow. Therefore, it will continue to get located by your DOM queries.

Without seeing your actual HTML, I can only speculate that what you're trying to accomplish can be done in a simpler way with the following jQuery loop:

$("tr input[value=" + mid + "]").each(function () {
    this.hide();
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文