Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Jquery中选择相邻父元素的子元素_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

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

Javascript 在Jquery中选择相邻父元素的子元素,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,现在,我有一个引导行设置了一个复选框,并在其中输入 <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" n

现在,我有一个引导行设置了一个复选框,并在其中输入

<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>
我尝试了next和nextAll,但据我所知,它们只能找到子元素。我需要知道如何选择第二个相邻父元素的子元素

为了简化这种情况:


该复选框有两个父元素(两个div)和一个相邻元素(另一个div列)。我需要访问另一个div列的子列,但它需要是通用的,因此我不需要为页面上的每个复选框/输入对创建8个函数。

您可能会想说:

$(this).parent().parent().next('div').find('input').
  css('visibility', 'visible');
由于这是通用的,因此不需要
each()


我不确定我是否同意接受的解决方案是稳定的,因为它在很大程度上取决于保持您的结构不变,我认为这在大多数项目中是不可能的。如果我可以建议一种替代方法,我认为这里更好的方法是使用选择器的scope参数

$('.feeswitch').on('switchChange.bootstrapSwitch', function () {
    var switchState = $(this).bootstrapSwitch('state') ? "visible" : "hidden";
    $('input',$(this).closest('.row')).not($(this)).css('visibility', switchState);
});
无论如何,您肯定应该更多地了解jQuery DOM遍历方法,因为这几乎就是jQuery的一大魔力:


感谢您通知我parent()方法。有了这条信息,我就可以访问我想要的输入表单。由于我拥有的插件以及它们如何生成标记,实际上需要大约5个parent()调用才能一直跳转到row div。从那里我可以得到子对象,对第二个对象执行.eq(1),然后使用.find('input')获得输入框。使用
.closest('.row')
而不是
.parent().parent()
@Devined您也可以看看
$('.feeswitch').on('switchChange.bootstrapSwitch', 
  function () {
    var visi = $(this).bootstrapSwitch('state') ? 'visible' : 'hidden';

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