简体   繁体   English

jQuery显示表行(如果找到值)

[英]JQuery show table row if value found

This might seem similar to other questions, and I have found and read through those questions and tried them, but I haven't gotten a working solution as of course, my code is unique and I need maybe another set of eyes to help me find my bug. 这似乎与其他问题类似,但我已经找到并阅读了这些问题并进行了尝试,但是我当然没有得到一个可行的解决方案,我的代码是唯一的,也许我需要另一组眼睛来帮助我查找我的虫子。

Anyway, the problem: 无论如何,问题是:

I'm creating a table (with PHP), and essentially if a cell has a value, the row gets an id of 'foo' and is hidden (using CSS - display: none;), and when I click a checkbox I'm trying to display all those hidden rows. 我正在创建一个表(使用PHP),基本上,如果一个单元格具有值,则该行的ID为'foo',并且被隐藏(使用CSS-display:none;),当我单击一个复选框时,我试图显示所有这些隐藏的行。

<tr id='foo'><td>this row is hidden</td></tr>
<tr><td>this row is not hidden</td></tr>

My jQuery is currently this: 我的jQuery当前是这样的:

$("#showHiddenRows").click(function() {
    if($(this).is(":checked")) {
        alert("showing"); //This pops up
        $("#tableName").each(function() {
            if($("#tableName").find("tr").attr("id") === 'foo') {
                $("#tableName").find("tr").attr("style", "display: initial;");
                alert("found a hidden row"); //This does not pop up...
            }
        });
    } else {
        alert("hiding");
        //essentially the same code here, but display:none;
    }

So I added alerts as you can see, the "showing" and "hiding" alerts pop up, but the "found a hidden row" doesn't, implying that searching through my table doesn't find any rows with an ID of 'foo', and I'm not sure why. 因此,您添加了警报,如您所见,弹出了“显示”和“隐藏”警报,但是“发现隐藏的行”却没有,这表明在我的表格中搜索没有找到ID为“ foo”,我不确定为什么。 Is there a better way to do this/is this the best way, and if so what did I do wrong? 有没有更好的方法可以做到这一点/这是最好的方法吗?如果是,我做错了什么?

Thanks in advance! 提前致谢!

Edit: Quick explanation of what I was intending the code to do: 编辑:我打算代码做什么的快速说明:

  • Loop through each row in the tableName table 遍历tableName表中的每一行
  • If you find a tablerow with an ID of 'foo', show or hide the row. 如果找到ID为'foo'的表格行,请显示或隐藏该行。

using non-unqiue id with jQuery will cause issues, but I'll leave that with you 在jQuery中使用非unidue id会导致问题,但我将留给您

here is what you should do to show hidden rows. 这是show隐藏行的方法。

$("#showHiddenRows").click(function() {
    if($(this).is(":checked")) {
      $("#tableName").each(function() {
        var table = $(this);
        var hiddenRows = table.find("tr[id=foo]").show();
        if(hiddenRows.length)
          alert("found " + hiddenRows.length + " hidden rows");
      });
    } else {
       alert("hiding");
    }
});

Do not use duplicate IDs ; 不要使用重复的ID use a CSS class instead. 改用CSS类。 By definition, IDs should be unique; 根据定义,ID应该是唯一的; therefore no two or more elements should have the same ID. 因此,任何两个或多个元素都不应具有相同的ID。

HTML: HTML:

<tr class='foo'><td>this row is hidden</td></tr>
<tr><td>this row is not hidden</td></tr>

JS: JS:

$(function() {
    $("#showHiddenRows").on('change', function() {
        if( this.checked ) {
            $('table tr.foo').show();
        } else {
            $('table tr.foo').hide();
        }
    });
});

Or, you could shorten it like this : 或者,您可以像这样缩短它

$(function() {
    $("#showHiddenRows").on('change', function() {
        $('table tr.foo')[ this.checked ? 'show' : 'hide' ]();
    });
});

Ended up with (thanks to user3558931): 最终得到了(感谢user3558931):

$(function() {
    $("#tableName").on('change', function() {
        if($(this).(":checked")) {
            $('#tableName tr#foo').show();
        } else {
            $('#tableName tr#foo').hide();
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM