如何使用JQuery获取堂兄元素? [英] How do I get to cousin elements with JQuery?

查看:70
本文介绍了如何使用JQuery获取堂兄元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多行数据的表,我想根据第一个元素中的复选框显示或隐藏每行的一些细节。例如:

I have a table with many rows of data, and I want to show or hide some of the details on each row based on a checkbox in the first element. For example:

<table>
  <tr>
    <td><span class="aspnetweirdness"><input type=checkbox></span></td>
    <td>Text Text Text</td>
    <td><select /></td>
    <td><input type=text></td>
  </tr>
</table>

我希望从复选框遍历到表兄弟元素(文本,选择和带有jquery的文本输入)并根据是否选中复选框切换这些元素的可见性。一个小障碍是复选框包含在一个范围内,因为这是由asp.net输出的。这也使得元素更难以通过id获得。

I'm looking to traverse from the checkbox to the cousin elements (the text, the select, and the text input) with jquery and toggle the visibility on those elements based on whether the checkbox is checked. One small snag is that the checkbox is wrapped in a span, since this is being output by asp.net. That also makes the elements harder to get at by id.

我将如何做到这一点?我已经尝试了$(this).parentsUntil('tr')。siblings(),但看起来我没有得到正确的元素。

How would I go about doing this? I've tried $(this).parentsUntil('tr').siblings(), but it doesn't seem like i get the right elements.

任何帮助我们将不胜感激。

Any help would be appreciated.

编辑:

 $(".crewMemberTable input:checkbox").toggle(function() {
            $(this).closest('tr').find('select, input:not(:checkbox)').fadeIn();
            $(this).closest('tr').find('label').css('font-weight', 'bold');
        }, function() {
            $(this).closest('tr').find('select, input:not(:checkbox)').fadeOut();
            $(this).closest('tr').find('label').css('font-weight', 'normal');
        });


推荐答案

你有没有尝试过:

$(this).closest('tr').find('td:not(:first-child)')

如果代码在点击处理程序中,this将是你的复选框元素。

where "this" would be your checkbox element if the code were in a "click" handler or something.

这篇关于如何使用JQuery获取堂兄元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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