[英]Show / hide nested checkbox labels
我试图使嵌套的标签/复选框被选中时可见。 我不知道如何定位特定的嵌套标签,或者这是否是正确的方法。
这是一个小提琴:
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();
}
});
});
如果使用了visibility:hidden;
.show()
不会显示元素visibility:hidden;
我会建议您使用
style="display:none;"
代替
style="visibility:hidden;"
关于此的一些想法:
for
属性正确)。 hide()
和show()
实际作用) 考虑到这一点,我建议以下内容。 JSFIDDLE : http : //jsfiddle.net/SaHGs/
HTML
<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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.