[英]How to show hide the element that was selected with closest and find method in JQuery?
I have form with check box and text area. 我有带有复选框和文本区域的表单。 If checkbox is check I want to show textarea if not hide.
如果复选框被选中,我想显示文本区域(如果不隐藏)。 Here is example of what I have:
这是我所拥有的示例:
$(document).on('click', '.dc-checkbox', setCheckboxVal); function setCheckboxVal() { var fldCheckbox = $(this); var fldComment = $(this).closest('div').find('.dc-comment'); console.log(fldComment); if (fldCheckbox.is(':checked')) { fldCheckbox.val(1); fldComment.show(); } else { fldCheckbox.val(0); fldComment.hide(); } }
.dc-comment { display: none; }
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <form id="myfrm" method="post"> <div class="checkbox"> <label for="checkbox1"> <input type="checkbox" name="dc_status1" id="dc_status1" class="dc-checkbox"> Checkbox 1</label> </div> <div class="form-group dc-comment"> <label for="comment1" class="pull-left">Comment:</label> <textarea class="form-control" rows="2" name="dc_comment1" id="dc_comment1"></textarea> </div> <div class="checkbox"> <label for="checkbox2"><input type="checkbox" name="dc_status2" id="dc_status2" class="dc-checkbox" value="1"> Checkbox 2</label> </div> <div class="form-group dc-comment"> <label for="comment2" class="pull-left">Comment:</label> <textarea class="form-control" rows="2" name="dc_comment2" id="dc_comment2"></textarea> </div> <div class="checkbox"> <label for="checkbox4"><input type="checkbox" name="dc_status4" id="dc_status4" class="dc-checkbox"> Checkbox 3</label> </div> <div class="form-group dc-comment"> <label for="comment4" class="pull-left">Comment:</label> <textarea class="form-control" rows="2" name="dc_comment4" id="dc_comment4"></textarea> </div> </form>
As you can see if I check the checkbox comment textarea is still not showing. 如您所见,如果我选中复选框,注释textarea仍未显示。 I'm not sure if
.closest()
and .find()
are methods that support show/hide in this case. 我不确定
.closest()
和.find()
在这种情况下是否支持show / hide。 Please let me know if you know the way to achieve this. 如果您知道实现此目标的方法,请告诉我。 Thank you.
谢谢。
$(this).closest('div')
finds the first parent div, in this case the one with class checkbox
- .find(..)
then looks in that div's children . $(this).closest('div')
查找第一个父div,在这种情况下,该类的checkbox
.find(..)
然后查找该div的子级 。 As the div class=checkbox
div doesn't have div class=dc-comment
as its child, it doesn't find it. 由于
div class=checkbox
div没有div class=dc-comment
作为其子级,因此找不到它。
.dc-comment
is a sibling (same level / same parent) as .checkbox
. .dc-comment
是同级(相同的水平/同一父)作为.checkbox
。
Change to .next()
or .nextAll(".dc-comment").first()
更改为
.next()
或.nextAll(".dc-comment").first()
Don't use .next(".dc-comment")
unless you know what it does as it may break (gets the next only if it matches, not the next that does match). 除非您知道它会做什么,否则不要使用
.next(".dc-comment")
,因为它可能会损坏(仅当匹配时才获取下一个,而不匹配时才获取)。
Updated snippet: 更新的代码段:
$(document).on('click', '.dc-checkbox', setCheckboxVal); function setCheckboxVal() { var fldCheckbox = $(this); var fldComment = $(this).closest('div').nextAll('.dc-comment').first(); //console.log(fldComment); if (fldCheckbox.is(':checked')) { fldCheckbox.val(1); fldComment.show(); } else { fldCheckbox.val(0); fldComment.hide(); } }
.dc-comment { display: none; }
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> <form id="myfrm" method="post"> <div class="checkbox"> <label for="checkbox1"> <input type="checkbox" name="dc_status1" id="dc_status1" class="dc-checkbox"> Checkbox 1</label> </div> <div class="form-group dc-comment"> <label for="comment1" class="pull-left">Comment:</label> <textarea class="form-control" rows="2" name="dc_comment1" id="dc_comment1"></textarea> </div> <div class="checkbox"> <label for="checkbox2"><input type="checkbox" name="dc_status2" id="dc_status2" class="dc-checkbox" value="1"> Checkbox 2</label> </div> <div class="form-group dc-comment"> <label for="comment2" class="pull-left">Comment:</label> <textarea class="form-control" rows="2" name="dc_comment2" id="dc_comment2"></textarea> </div> <div class="checkbox"> <label for="checkbox4"><input type="checkbox" name="dc_status4" id="dc_status4" class="dc-checkbox"> Checkbox 3</label> </div> <div class="form-group dc-comment"> <label for="comment4" class="pull-left">Comment:</label> <textarea class="form-control" rows="2" name="dc_comment4" id="dc_comment4"></textarea> </div> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.