简体   繁体   English

使用jQuery基于复选框显示/隐藏div

[英]Showing/hiding divs based on checkbox using jQuery

I have a form with a checkbox (contained within a label #contact ). 我有一个带有复选框的表单(包含在#contact标签中)。 For the change action of this checkbox, I am showing/hiding a div ( #options ). 对于此复选框的更改操作,我正在显示/隐藏div( #options )。

To ensure that if the checkbox is checked the #options div always is always shown (and avoid the situation where checking the checkbox actually hides the subjequent options), I am using this code: 为了确保始终选中此复选框,始终显示#options div(并避免选中该复选框实际上隐藏了后续选项的情况),我使用以下代码:

$('#contact :checkbox').is(':checked') ? $("#options").show() : $("#options").hide();

This works fine. 这很好。 The problem I have is that instead of a single checkbox with an ID, I want to have multiple checkboxes. 我的问题是,我希望有多个复选框,而不是具有ID的单个复选框。 I want to show/hide the next instance of my .hidden class based on whether the previous checkbox (within a label with the class .trigger ) is checked or not. 我想根据是否选中上一个复选框(在带有.trigger类的标签中),显示/隐藏我的.hidden类的下一个实例。 I have tried this: 我已经试过了:

$(document).ready(function() {
    if( $('.trigger :checkbox').is(':checked') ) {
        $(this).parent().nextAll('ul.hidden').show();
    } else {
        $(this).parent().nextAll('ul.hidden').hide();
    }
});

But to no avail. 但无济于事。 The checkboxes are in an unordered list, like this: 复选框位于无序列表中,如下所示:

<ul>
  <li><label class="trigger"><input type="checkbox" name="02" /> Trigger 1</label>
      <ul class="hidden">
        <li><label><input type="checkbox" name="02-sub1" /> Normal</label></li>
        <li><label><input type="checkbox" name="02-sub2" /> Normal</label></li>
      </ul>
  </li>
  <li><label><input type="checkbox" name="02" /> Normal</label></li>
  <li><label><input type="checkbox" name="03" /> Normal</label></li>
  <li><label class="trigger"><input type="checkbox" name="04" /> Trigger 2</label>
      <ul class="hidden">
        <li><label><input type="checkbox" name="04-sub1" /> Normal</label></li>
        <li><label><input type="checkbox" name="04-sub2" /> Normal</label></li>
      </ul>
  </li>
</ul>

I can't see where I'm going wrong here; 我看不到我要去哪里错了; presumably my selector is incorrect, but I've played around with the syntax for ages and not got anywhere. 大概我的选择器是不正确的,但是我已经使用了很长时间的语法,而且一无所获。 Thanks in advance (and thank you for reading this far). 在此先感谢您(并感谢您阅读本文)。

You need to run your code inside a change handler, so this refers to the checkbox you want, like this: 您需要在change处理程序中运行您的代码,因此this是指您想要的复选框,如下所示:

$(document).ready(function() {
   $('.trigger :checkbox').change(function() {
     if( $(this).is(':checked') ) {
       $(this).parent().nextAll('ul.hidden').show();
     } else {
       $(this).parent().nextAll('ul.hidden').hide();
     }
   });
});

...and that can be made much shorter with .toggle(bool) , like this: ...并且可以使用.toggle(bool)使其变得更短,如下所示:

$(function() {
   $('.trigger :checkbox').change(function() {
     $(this).parent().nextAll('ul.hidden').toggle(this.checked);
   });
});

If you need it to run when the page loads, so the show/hide states match the checkbox, just call that change handler with .change() (shortcut for .trigger('change') ), like this: 如果您需要它在页面加载时运行,以便显示/隐藏状态与复选框匹配,只需使用.change() (. .trigger('change')快捷方式.change()调用该change处理程序,如下所示:

$(function() {
   $('.trigger :checkbox').change(function() {
     $(this).parent().nextAll('ul.hidden').toggle(this.checked);
   }).change();
});

You know you can roll your own fields, right? 您知道您可以滚动自己的字段,对吗? The typical way to do that is with a "rel=" field that has no meaning in HTML but can be picked up and used in jquery. 典型的方法是使用“ rel =”字段,该字段在HTML中没有任何意义,但可以在jquery中使用。

if ($('#contact :checkbox').is(':checked')) {
    $("#" + $('#contact :checkbox').attr('rel')).show();
} else {
    $("#" + $('#contact :checkbox').attr('rel')).hide();
}

And then: 接着:

  <li><label class="trigger"><input type="checkbox" name="04" rel="hidden-04" /> Trigger 2</label>
      <ul class="hidden" id="hidden-04">
        <li><label><input type="checkbox" name="04-sub1" /> Normal</label></li>
        <li><label><input type="checkbox" name="04-sub2" /> Normal</label></li>
      </ul>
  </li>

So then, when the checkbox named 04 is checked, we look in its rel field for the id of the object to hide() or show() . 因此,当选中名为04的复选框时,我们在其rel字段中查找对象的id到hide()show() I think that's a LOT easier than trying to walk the dom to find the target object. 我认为这比尝试遍历dom来查找目标对象要容易得多。

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

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