0
$(document).ready(function (e) {
    $('.links > input').change(function () {
        if ($(this).is(':checked')) {
            $(this).parent().children('.subLinks').children('.subLinkCheck').children().attr('checked', true);
        } else 
          $(this).parent().children('.subLinks').children('.subLinkCheck').children().attr('checked', false);;
    });
});

它第一次工作正常。当我取消选中子复选框然后单击父复选框时,它会失败,

这是 HTML 内容

<ul id="mainLink">
    <li class="links">
        <input type="checkbox" />Interactive Community
        <ul class="subLinks">
            <li class="subLinkCheck">
                <input class="1" type="checkbox" />Usability Testing</li>
            <li class="subLinkCheck">
                <input type="checkbox" />HFI Certification</li>
        </ul>
    </li>
    <li class="links">
        <input type="checkbox" />Heuristic Evaluations
        <ul class="subLinks">
            <li class="subLinkCheck">
                <input type="checkbox" />Usability Testing</li>
            <li class="subLinkCheck">
                <input type="checkbox" />HFI Certification</li>
        </ul>
    </li>
</ul>
4

1 回答 1

4

使用.prop()而不是.attr().

演示:http: //jsfiddle.net/Jrrnj/

于 2013-04-24T10:26:27.647 回答