简体   繁体   English

在Jquery中选择相邻父元素的子元素

[英]Selecting child of adjacent parent elements in Jquery

Right now I have aa bootstrap row set up with a checkbox and input inside of it. 现在我有一个带有选择框的引导行,并在其中输入。

<div class="row">
<div class="AdminFees">
    <div class="col-md-2"><input type="checkbox" class="make-switch feeswitch" data-on-color="info" data-off-color="info" data-on-text="Yes" data-off-text="No" name="HasPerMemberFee" checked="@Model.Contract.HasPerMemberFee" /></div>
</div>
<div class="col-md-2"><input type="text" class="form-control" placeholder="$0.00" id="PerMemberFeeAmount" name="PerMemberFeeAmount" value="@Model.Contract.PerMemberFeeAmount" /></div>

I have 6 or 7 very identical field pairs that I am manipulating generically so I don't have to make multiple functions. 我有6或7个非常相似的字段对,可以通用地进行操作,因此不必执行多个功能。 I am having trouble figuring out how to reference the text input element from the checkbox element. 我在弄清楚如何从复选框元素引用文本输入元素时遇到麻烦。

Jquery: jQuery:

$('.feeswitch').each(function () {
    $(this).on('switchChange.bootstrapSwitch', function () {
        if ($(this).bootstrapSwitch('state') == true)
            $(this).next('input').css('visibility', 'visible');
        else
            $(this).next('input').css('visibility', 'hidden');
    });
});

I have tried next and nextAll, but to my understanding, those only find child elements. 我尝试了next和nextAll,但是据我所知,那些只找到子元素。 I need to know how to select the child of the second adjacent parent element. 我需要知道如何选择第二个相邻父元素的子元素。

In an attempt to simplify the situation: 为了简化情况:

The checkbox has 2 parents, the two divs, and 1 adjacent element, the other div column. 该复选框具有2个父级(两个div)和1个相邻元素(另一个div列)。 I need to access the child of that other div column, but it needs to be generically so I don't need to make 8 functions for each checkbox/input pair on my page. 我需要访问该其他div列的子级,但是它需要具有一般性,因此我不需要为页面上的每个复选框/输入对设置8个函数。

I'm not sure I agree that the accepted solutions is stable, since it's selector greatly depends on keeping your structure the same, which I would suggest is unlikely in most projects. 我不确定我是否同意所接受的解决方案是否稳定,因为它的选择器很大程度上取决于保持您的结构不变,这在大多数项目中都不太可能。 If I might suggest an alternative, I think the better method here is to employ the scope parameter of the selector. 如果我建议使用其他方法,我认为这里更好的方法是采用选择器的scope参数。

$('.feeswitch').on('switchChange.bootstrapSwitch', function () {
    var switchState = $(this).bootstrapSwitch('state') ? "visible" : "hidden";
    $('input',$(this).closest('.row')).not($(this)).css('visibility', switchState);
});

Regardless, you should definitely look more in to jQuery DOM traversal methods, as that's pretty much the big magic of jQuery: 无论如何,您绝对应该更多地关注jQuery DOM遍历方法,因为这几乎就是jQuery的一大魔力:

https://api.jquery.com/category/traversing/ https://api.jquery.com/category/traversing/

You'll want to say something like: 您将要说些类似的话:

$(this).parent().parent().next('div').find('input').
  css('visibility', 'visible');

And since this is generic, you don't need the each() : 并且由于这通用的,因此您不需要each()

$('.feeswitch').on('switchChange.bootstrapSwitch', 
  function () {
    var visi = $(this).bootstrapSwitch('state') ? 'visible' : 'hidden';

    $(this).parent().parent().next('div').find('input').
      css('visibility', visi);
  }
);

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

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