gpt4 book ai didi

javascript - 如何在JQuery中显示隐藏使用最接近和查找方法选择的元素?

转载 作者:行者123 更新时间:2023-12-03 00:12:10 25 4
gpt4 key购买 nike

我有带有复选框和文本区域的表单。如果选中复选框,我想显示文本区域(如果不隐藏)。这是我所拥有的示例:

$(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>

如您所见,如果我选中复选框注释文本区域仍然没有显示。我不确定 .closest().find() 是否是在这种情况下支持显示/隐藏的方法。如果您知道实现此目标的方法,请告诉我。谢谢。

最佳答案

$(this).closest('div') 查找第一个父 div,在本例中为 checkbox 类 - .find(. .) 然后查看该 div 的。由于 div class=checkbox div 没有 div class=dc-comment 作为其子级,因此找不到它。

.dc-comment.checkbox 的同级(同一级别/同一父级)。

更改为 .next().nextAll(".dc-comment").first()

不要使用 .next(".dc-comment") 除非你知道它的作用,因为它可能会中断(仅在匹配时获取下一个,而不是匹配的下一个) .

更新的代码片段:

$(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>

关于javascript - 如何在JQuery中显示隐藏使用最接近和查找方法选择的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54675865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com