当选中包含label/复选框时,我试图使嵌套的label/复选框可见。我不知道如何针对特定的嵌套标签,或者这是否是正确的方法。
这里有一把小提琴:
http://jsfiddle.net/kirkbross/tfKva/15/
以下是javascript:
$(".label-1").click(function() {
$("input:checkbox").each(function() {
if ($(this).prop("checked")) {
$(this).next('.label-2').show(); // can't figure out how to get at the second "nested" label
} else {
$(this).next('.label-2').hide();
}
});
});
发布于 2014-06-12 17:55:14
关于这一点的几点想法:
for
属性,则会导致问题),您的html结构可能会更好一些。hide()
和show()
实际上是这样做的)。考虑到这一点,我建议如下。JSFIDDLE:http://jsfiddle.net/SaHGs/
<div class="check-container">
<label for="label1">This is label 1</label>
<input type="checkbox" id="label1" class="hideshow" />
<div class="autohide checkhidden">
<label for="label2">This is label 2</label>
<input type="checkbox" id="label2" />
</div>
</div>
Javascript
$(function () {
$(".hideshow").click(function(args) {
var showStuff = $(this).prop("checked");
$(this).parent().find(".autohide").each(function () {
if (showStuff)
$(this).removeClass("checkhidden");
else
$(this).addClass("checkhidden");
});
});
});
CSS
ul { list-style:none; }
li {margin-bottom:20px; }
.check-container {
position: relative;
display:block;
width:300px;
height:200px;
background: #e8e8e8;
}
.checkhidden { display: none; }
希望这能帮上忙!
编辑
为了检查和取消选中容器中的第一项,只需在javascript中添加以下两个函数:
$(".check-container").click(function () {
var el = $(this).find("input:first");
el.prop("checked", !el.prop("checked")).change();
});
$("input, label").click(function (e) {
e.stopPropagation();
});
发布于 2014-06-12 17:40:26
如果您使用了.show()
,则visibility:hidden;
不会显示元素
我建议你用
style="display:none;"
而不是
style="visibility:hidden;"
DEMO
https://stackoverflow.com/questions/24190532
复制相似问题