【问题标题】:hide textbox if jquery list has the class如果 jquery 列表具有类,则隐藏文本框
【发布时间】:2015-07-16 17:25:46
【问题描述】:

当在 UL > LI 中找到活动的类时,我如何隐藏文本框

当我应该显示它的标签和文本框时,它应该被隐藏。我正在尝试获取选定复选框的值并将其隐藏但无法到达那里。

$('ul.multiselect-container').has('li.active').css('display', 'none');

<ul class="multiselect-container dropdown-menu">
    <li class="active"><a tabindex="0"><label class="checkbox"><input type="checkbox" value="1"> Paypal</label></a>

    </li>
    <li class="active"><a tabindex="0"><label class="checkbox"><input type="checkbox" value="2"> Stripe</label></a>

    </li>
    <li class=""><a tabindex="0"><label class="checkbox"><input type="checkbox" value="3">Beanstream</label></a>

    </li>
</ul>
<label for="Configuration_paypalId" class="control-label">Paypal Id</label>
<input type="text" value="" id="Configuration_paypalId" name="Configuration[paypalId]" maxlength="60" class="span4">
<br>
<label for="Configuration_stripeId" class="control-label">Stripe Id</label>
<input type="text" value="" id="Configuration_stripeId" name="Configuration[stripeId]" maxlength="60" class="span4">
<br>
<label for="Configuration_beanId" class="control-label">Bean Id</label>
<input type="text" value="" id="Configuration_beanId" name="Configuration[beanId]" maxlength="60" class="span4">

更新

抱歉,如果问题不清楚, 当我单击第一个复选框时,说贝宝,然后只有贝宝标签和文本框应该出现在下面,其余的文本框 2 及其标签应该隐藏。我猜我们需要用 each() 循环遍历 UL 标记。

这是一个小提琴。 http://jsfiddle.net/nzw7LoL0/5/

更新 2

我无法向名为 option 和 option 1 的 div 添加一个类,因为 html 是使用框架生成的。所以我需要隐藏具有.control-grouphttp://jsfiddle.net/d280wqr4/5/类的行

$('input:checkbox').on('change', function () { // Apply change listener to checkboxes
    if ($(this).is(':checked')) { // If this checkbox is checked, show the relevant element
        $('.option' + $(this).prop('value'))find('.control-group').show();
    } else { // Otherwise, hide the relevant element
        $('.option' + $(this).prop('value')).find('.control-group')hide();
    }
});


<div class="control-group "><label for="Configuration_paypalId" class="control-label">Paypal Id</label><div class="controls"><input type="text" value="" id="Configuration_paypalId" name="Configuration[paypalId]" maxlength="60" class="span4 option option1"><p class="help-block">Merchant id or email of the Paypal account</p></div></div>

    <div class="control-group "><label for="Configuration_stripeId" class="control-label">Stripe Id</label><div class="controls"><input type="text" id="Configuration_stripeId" name="Configuration[stripeId]" maxlength="60" class="span4 option option2"><p class="help-block">Stripe id or email of the Stripe account</p></div></div>

【问题讨论】:

  • 所有这些输入字段?
  • @ArunPJohny 当我点击第一个复选框时,说贝宝,然后应该只出现贝宝标签和文本框,其余的文本框 2 及其标签应该隐藏。我猜我们需要用 each() 循环遍历 UL 标签
  • 你的问题需要我澄清一下,我们很难理解你真正想要什么?
  • @JoshStevens 请看这个小提琴。 jsfiddle.net/nzw7LoL0/8 当我点击复选框 Paypal Id 标签中的贝宝时,它的文本框应该存在。剩下的 2 个文本框和标签应该隐藏。我够清楚了吗?

标签: javascript jquery html css


【解决方案1】:

分配一个通用类,让选择器变得简单

<input type="text" value="" id="Configuration_beanId" name="Configuration[beanId]" maxlength="60" class="span4 classname">

然后

$('input.classname').toggle($('.multiselect-container li.active').length == 0);//hides input with class `classname` if `li` has `active` class
$('ul.multiselect-container').has('li.active').hide();//hides the ul if li has active class

$('.multiselect-container input:checkbox').click(function(){
  var type = $(this).data('type');
  $('.'+type).toggle(this.checked);
  $(this).closest('li').addClass('active')
});

$('.payid').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul class="multiselect-container dropdown-menu">
  <li><a tabindex="0"><label class="checkbox"><input type="checkbox" value="1" data-type="paypal"> Paypal</label></a></li>
  <li><a tabindex="0"><label class="checkbox"><input type="checkbox" value="2" data-type="stripe"> Stripe</label></a></li>
  <li class=""><a tabindex="0"><label class="checkbox"><input type="checkbox" value="3" data-type="bean">Beanstream</label></a></li>
</ul>
<div class="paypal payid">
  <label for="Configuration_paypalId" class="control-label">Paypal Id</label>
  <input type="text" value="" id="Configuration_paypalId" name="Configuration[paypalId]" maxlength="60" class="span4">
</div>
<div class="stripe payid">
  <label for="Configuration_stripeId" class="control-label">Stripe Id</label>
  <input type="text" value="" id="Configuration_stripeId" name="Configuration[stripeId]" maxlength="60" class="span4">
</div>
<div class="bean payid">
  <label for="Configuration_beanId" class="control-label">Bean Id</label>
  <input type="text" value="" id="Configuration_beanId" name="Configuration[beanId]" maxlength="60" class="span4">
</div>

【讨论】:

  • 当我点击第一个复选框时,说贝宝。那么只有贝宝标签和文本框应该出现,其余的文本框 2(条纹和 bean)及其标签应该隐藏。我猜我们需要用 each() 循环遍历 UL 标签
  • @dev1234 尝试更新 - 也有 html 更改
  • @dev1234 你可以有一个处理程序来添加类并设置可见性
  • 哦,好的。你能提供一个js小提琴吗?
  • @ArunPJohny 这需要是更改侦听器而不是点击侦听器。仍然可以单击禁用的复选框。也不需要数据属性。
【解决方案2】:

如果您能够稍微更改您的标记,那么您可以通过这种方式使用data-* 属性:

    $('.dropdown-menu :checkbox').change(function(e) {
      var target = $(this).data('target');
      $('.' + target).toggle();
    });
.paypal, .stripe, .beanstream{display:none;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="multiselect-container dropdown-menu">
  <li class="">
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" data-target='paypal' value="1">Paypal</label>
    </a>
  </li>
  <li class="">
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" data-target='stripe' value="2">Stripe</label>
    </a>
  </li>
  <li class="">
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" data-target='beanstream' value="3">Beanstream</label>
    </a>
  </li>
</ul>
<label for="Configuration_paypalId" class="control-label paypal">Paypal Id</label>
<input type="text" value="" id="Configuration_paypalId" name="Configuration[paypalId]" maxlength="60" class="span4 paypal">
<br>
<label for="Configuration_stripeId" class="control-label stripe">Stripe Id</label>
<input type="text" value="" id="Configuration_stripeId" name="Configuration[stripeId]" maxlength="60" class="span4 stripe">
<br>
<label for="Configuration_beanId" class="control-label beanstream">Bean Id</label>
<input type="text" value="" id="Configuration_beanId" name="Configuration[beanId]" maxlength="60" class="span4 beanstream">

【讨论】:

  • 最好检查复选框是否被选中并根据它执行操作,否则您可能会打开它们变得不同步的可能性,并且“选中”复选框会隐藏它而不是显示它.
  • @JamieBarker 是吗?但由于 OP 更新了问题,所以似乎 radios 是更好的选择。
  • 是的,您正在根据每个更改事件切换可见性。如果其中一个复选框由于某种原因(可能是浏览器后退按钮缓存或其他原因)以选中状态启动页面,那么它将无法正常工作。
  • 你的答案很有效,它只是有你可能没有考虑到的潜在缺陷:)
【解决方案3】:

您需要对复选框应用“更改”侦听器,然后根据此信息确定它们是否被选中并显示/隐藏另一个元素。您确实需要一个与复选框的 value 属性匹配的类或 id。

下面的例子应该如你所愿。

$('input:checkbox').on('change', function() { // Apply change listener to checkboxes
  if ($(this).is(':checked')) { // If this checkbox is checked, show the relevant element
    $('.option' + $(this).prop('value')).show();
  } else { // Otherwise, hide the relevant element
    $('.option' + $(this).prop('value')).hide();
  }
});
.option {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="multiselect-container dropdown-menu">
  <li>
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" value="1" />Paypal
      </label>
    </a>
  </li>
  <li>
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" value="2" />Stripe
      </label>
    </a>
  </li>
  <li>
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" value="3" />Beanstream
      </label>
    </a>
  </li>
</ul>
<div class="option option1"> <!-- The number refers to the value of the checkbox -->
  <label for="Configuration_paypalId" class="control-label">Paypal Id</label>
  <input type="text" value="" id="Configuration_paypalId" name="Configuration[paypalId]" maxlength="60" class="span4">
</div>
<div class="option option2">
  <label for="Configuration_stripeId" class="control-label">Stripe Id</label>
  <input type="text" value="" id="Configuration_stripeId" name="Configuration[stripeId]" maxlength="60" class="span4">
</div>
<div class="option option3">
  <label for="Configuration_beanId" class="control-label">Bean Id</label>
  <input type="text" value="" id="Configuration_beanId" name="Configuration[beanId]" maxlength="60" class="span4">
</div>

根据更多信息更新

如果您无法编辑 html,因为它是动态生成的,您需要根据索引进行编辑。这仅在复选框的顺序与其他输入匹配时才有效,但如果它是动态生成的,那么我不明白为什么不应该是这种情况。

$('input:checkbox').on('change', function() { // Apply change listener to checkboxes
  var index = parseInt($(this).prop('value')) - 1; // Make the index zero based
  if ($(this).is(':checked')) { // If this checkbox is checked, show the relevant element
    $('.control-group:eq(' + index + ')').show();
  } else { // Otherwise, hide the relevant element
    $('.control-group:eq(' + index + ')').hide();
  }
});
.control-group {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="multiselect-container dropdown-menu">
  <li>
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" value="1" />Paypal
      </label>
    </a>
  </li>
  <li>
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" value="2" />Stripe
      </label>
    </a>
  </li>
  <li>
    <a tabindex="0">
      <label class="checkbox">
        <input type="checkbox" value="3" />Beanstream
      </label>
    </a>
  </li>
</ul>
<p>------------------------------</p>
<div class="control-group">
  <label for="Configuration_paypalId" class="control-label">Paypal Id</label>
  <input type="text" value="" id="Configuration_paypalId" name="Configuration[paypalId]" maxlength="60" class="span4 option option1" />
  <p class="help-block">Merchant id or email of the Paypal account</p>
</div>
<div class="control-group">
  <label for="Configuration_stripeId" class="control-label">Stripe Id</label>
  <input type="text" id="Configuration_stripeId" name="Configuration[stripeId]" maxlength="60" class="span4 option option2" />
  <p class="help-block">Stripe id or email of the Stripe account</p>
</div>

【讨论】:

  • 您好,您是否可以更改代码以在 class 为 control-group 时隐藏该行。检查这个jsfiddle.net/d280wqr4/4
  • 由于生成了 HTML,因此很难添加名为 option 和 option 1 的类。所以我已将其添加到文本字段中,但我也想隐藏 div 标签。请检查我最后的小提琴。
  • 唯一的方法是使用索引:jsfiddle.net/d280wqr4/6。只要复选框的顺序与输入的顺序匹配,这将起作用。
  • 一切都很好,但我无法使用这种风格 .control-group { display: none; } 因为表单中还有其他行也具有相同的 div 类。如何隐藏具有 paypalId 和 stripeId 的行 div.control-group。
  • @dev1234 它们在自己的父容器中吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 2011-02-01
  • 2013-05-08
  • 1970-01-01
相关资源
最近更新 更多