jQuery: Hide parent <td> if contains child <input> with specific class?

Deadly 提交于 2019-12-13 04:44:01

问题


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();
}
});


来源:https://stackoverflow.com/questions/16532109/jquery-hide-parent-td-if-contains-child-input-with-specific-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!