我有这样的代码:
<ul>
<li><input type="checkbox" level="child" name="somename1" value="1" /> Child 1</li>
<li><input type="checkbox" level="child" name="somename2" value="2" /> Child 2 <br />
<ul>
<li><input type="checkbox" level="subchild" name="somename1" value="3" />Sub Child 1</li>
<li><input type="checkbox" level="subchild" name="somename2" value="3" />Sub Child 2</li>
</ul>
</li>
<li ><input type="checkbox" level="child" name="somename3" value="3" /> Child 3</li>
</ul>
和jQuery代码:
$('input[@type=checkbox][level="child"]').click(function (event) {
var checked = $(this).is(':checked');
if (checked) {
alert("checked");
} else { //for click child to uncheck subchild
alert("when unchecked");
$(this).closest('ul > li').children('input[@type=checkbox][level="subchild"]').attr('checked', false);
}
});
情况: child 2 + subchild1 + subchild2 被选中
问题:当我单击取消选中子 2 时,我想取消选中 subchild1 + subchild2,我上面的代码不起作用。我认为这与:
$(this).closest('ul > li').children('input[@type=checkbox [level="subchild"]').attr('checked', false);
任何帮助都会很棒。这是 JSfiddle:http: //jsfiddle.net/p324w/