简体   繁体   English

如何使用JQuery获取堂兄元素?

[英]How do I get to cousin elements with 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>

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. 我希望使用jquery从复选框遍历表达元素(文本,选择和文本输入),并根据是否选中复选框切换这些元素的可见性。 One small snag is that the checkbox is wrapped in a span, since this is being output by asp.net. 一个小障碍是复选框包含在一个范围内,因为这是由asp.net输出的。 That also makes the elements harder to get at by id. 这也使得元素更难以通过id获取。

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. 我已经尝试了$(this).parentsUntil('tr')。siblings(),但它似乎并没有得到正确的元素。

Any help would be appreciated. 任何帮助,将不胜感激。

EDIT: 编辑:

 $(".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');
        });

Well have you tried: 你有没有尝试过:

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

where "this" would be your checkbox element if the code were in a "click" handler or something. 如果代码在“点击”处理程序或其他东西,“this”将是你的复选框元素。

I know this is an old question, but I just stumbled across it and realized I recently wrote a generic function for this. 我知道这是一个老问题,但我偶然发现它并意识到我最近为此编写了一个通用函数。

Using the function below you could simply write $(this).cousins() to get a collection containing the text, select, and text-input elements (where this is of course your checkbox.) 使用下面的函数你可以简单地写$(this).cousins()来获得一个包含text,select和text-input元素的集合(当然this是你的复选框。)

/* See http://addictedtonew.com/archives/414/creating-a-jquery-plugin-from-scratch/
 * Used like any other jQuery function:
 *        $( selector ).cousins()
 */
(function($) {
    $.fn.cousins = function() {
        var cousins;
        this.each(function() {
            var auntsAndUncles = $(this).parent().siblings();
            auntsAndUncles.each(function() {
                if(cousins == null) {
                    cousins = auntsAndUncles.children();
                }
                else cousins.add( auntsAndUncles.children() );
            });
        });
        return cousins;
    }
})(jQuery)
$('input[type=checkbox]').click(function() {
  $(this).closest('td').siblings().find('select,input').hide();
});

I am new to stackoverflow and I'm not sure if I can or not, but I edited the answer of @robjb and posting here. 我是stackoverflow的新手,我不确定我能否,但我编辑了@robjb的答案并在此发布。 Full credit goes to him only. 完全归功于他。

In case if anyone wants to select only particular cousins and not all cousins (ie wants to have selector), then we can use this modified version of @robjb 's answer. 如果有人想要只选择特定的堂兄弟而不是所有堂兄弟(即想要选择器),那么我们可以使用@robjb答案的这个修改版本。 If selector is not passed as an argument then it will give all cousins. 如果选择器不作为参数传递,那么它将给出所有表兄弟。

(function($) {
$.fn.cousins = function(selector) {
    var cousins;
    this.each(function() {
        var auntsAndUncles = $(this).parent().siblings();
        auntsAndUncles.each(function() {
            if(cousins == null) {
                if(selector)
                    cousins = auntsAndUncles.children(selector);
                else
                    cousins = auntsAndUncles.children();
            }
            else {
                if(selector)
                    cousins.add( auntsAndUncles.children(selector) );
                else
                    cousins.add( auntsAndUncles.children() );
            }
        });
    });
    return cousins;
    }
})(jQuery)

Example use of the above function would be as 上述功能的使用示例如下

$(clickedObj).cousins('.singleWheel');

The other option would be to use .filter() and use @robjb's answer. 另一种选择是使用.filter()并使用@ robjb的答案。

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

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