[英]jQuery - show and hide div related to different checkbox
Hello I'm fairly new to jQuery (and stackoverflow), i saw a few questions about showing/hiding div for checkbox but i couldn't find a solution that apply for several checkboxes at the same time.您好,我是 jQuery(和 stackoverflow)的新手,我看到了一些关于显示/隐藏复选框的 div 的问题,但我找不到同时适用于多个复选框的解决方案。
I would like to make it work so that when my first checkbox is checked, the 1st div/panel just after is shown then hiden when uncheck.我想让它工作,以便当我的第一个复选框被选中时,紧接着的第一个 div/面板显示然后在取消选中时隐藏。 And the same thing for my second checkbox with its own panel.
我的第二个复选框也有自己的面板。
Right now, both panel are shown when first checkbox (or second checkbox) is checked.现在,当第一个复选框(或第二个复选框)被选中时,两个面板都会显示。 I don't know how to make it so the panel shown or hidden will be dependant of the checkbox (li?).
我不知道如何制作它,所以显示或隐藏的面板将取决于复选框(li?)。
I would like a solution that could apply without touching the code when adding new checkboxes, so i think maybe ID shouldn't be use, right?我想要一个在添加新复选框时无需触及代码即可应用的解决方案,所以我认为也许不应该使用 ID,对吗? is there any solution?
有什么解决办法吗?
Your help is much appreciated!非常感谢您的帮助! Thanks in advance!
提前致谢!
HTML: HTML:
<ul class="form-check-list">
<li class="js-tabCheck">
<label for="coupon" class="check">
<input type="checkbox" id="coupon" class="js-tabCheck-trigger">
<span class="check__name txt-bold">COUPON</span>
<span class="txt-notice txt-indent">-50%</span>
</label>
<div class="js-tabCheck-panel">
<p>coupon detail</p>
</div>
</li>
<li class="js-tabCheck">
<label for="coupon1" class="check">
<input type="checkbox" id="coupon1" class="js-tabCheck-trigger">
<span class="check__name txt-bold">COUPON1</span>
<span class="txt-notice txt-indent">-30%</span>
</label>
<div class="js-tabCheck-panel">
<p>coupon detail</p>
</div>
</li>
</ul>
JQUERY: JQUERY:
$(function(){
var checkbox = $('.js-tabCheck-trigger'),
panel = $('.js-tabCheck-panel');
panel.hide();
checkbox.change(function(){
if($(this).is(':checked')){
panel.show();
} else {
panel.hide();
};
});
});
by using the children method, you can call the respective div element.通过使用 children 方法,您可以调用相应的 div 元素。 and used 'parents' to access the outside element.
并使用“父母”访问外部元素。
$(function(){ var checkbox = $('.js-tabCheck-trigger'), panel = $('.js-tabCheck-panel'); panel.hide(); checkbox.change(function(){ if($(this).is(':checked')){ $(this).parents('li').children('.js-tabCheck-panel').show(); } else { $(this).parents('li').children('.js-tabCheck-panel').hide(); }; }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="form-check-list"> <li class="js-tabCheck"> <label for="coupon" class="check"> <input type="checkbox" id="coupon" class="js-tabCheck-trigger"> <span class="check__name txt-bold">COUPON</span> <span class="txt-notice txt-indent">-50%</span> </label> <div class="js-tabCheck-panel"> <p>coupon detail</p> </div> </li> <li class="js-tabCheck"> <label for="coupon1" class="check"> <input type="checkbox" id="coupon1" class="js-tabCheck-trigger"> <span class="check__name txt-bold">COUPON1</span> <span class="txt-notice txt-indent">-30%</span> </label> <div class="js-tabCheck-panel"> <p>coupon detail</p> </div> </li> </ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.