jQuery: Hide parent if contains child <inp

2019-09-13 17:46发布

问题:

I'm working with the jQuery Tablesorter Filters plugin and I'm trying to hide certain <td>. This is what I'm looking to do:

-If <td> contains <input class="tablesorter-filter disabled">, hide the parent <td>

<tr class="tablesorter-filter-row">
    <td>
        <input class="tablesorter-filter">
    </td>

    <td>
        <input class="tablesorter-filter disabled">
    </td>

    <td>
        <input class="tablesorter-filter">
    </td>
</tr>

<script>
    jQuery(function ($) {
        $(".tablesorter-filter-row td").filter(function(i) { return $(this).find("> input.disabled").length > 0 }).hide();
    });
</script>

That is the jQuery I culled from another issue I had, but it's not hiding the parent td in this instance.

回答1:

Your code look fine. Make sure you do this in DOM ready handler and it should work fine.

$(function() {
   $(".tablesorter-filter-row td").filter(function() {
         return $('input', this).hasClass('disabled');
    }).hide();
});

Check Fiddle



回答2:

$('.tablesorter-filter-row td').each(function(){
if($(this).children('input').hasClass('disabled').length == 0)
{
    $(this).children('input').hide();
}
});