【发布时间】:2011-06-03 18:42:40
【问题描述】:
我是 Javascript 和 JQuery 的新手,所以我一直在阅读它,并尝试在选中其中一个子项时检查(并设置为非活动)父列表项中的复选框。
如果这没有任何意义,请查看列表结构。
<ul>
<li><input type="checkbox" name="items[]" value="88712003" id="88712003" /> Parent 1</li>
<li><input type="checkbox" name="items[]" value="88712003" id="88712003" /> Parent 2
<ul>
<li><input type="checkbox" name="items[]" value="21312341" id="21312341" /> Child 1</li>
<li><input type="checkbox" name="items[]" value="21312341" id="21312341" /> Child 2</li>
</ul>
</li>
<li><input type="checkbox" name="items[]" value="88712003" id="88712003" /> Parent 3</li>
<li><input type="checkbox" name="items[]" value="88712003" id="88712003" /> Parent 4</li>
</ul>
如果选中 Child 1 或 Child 2,我希望检查 Parent 2 中的输入并将其设置为非活动状态。我已经开始研究它,但被困在这里:
$(function(){
$('.child').click(function() {
$(this).parent().parent().parent().toggle();
});
});
如你所见,我没走多远。任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
请注意,有一个名为
indeterminate的方便但非标准的复选框属性可能适合您在这种情况下设置。所有主流浏览器都支持它。这是在 jQuery 中设置它的示例。 jsfiddle.net/L2uDL
标签: jquery list input checkbox