Jquery选择最近的复选框收件箱不起作用

时间:2012-10-17 12:40:30

标签: jquery checkbox selector closest

我有一个treelist包含多个级别的checkboxes,我试图让父级checkbox在任何子级'checked'时显示为checkboxes点击。由于某些原因,这不起作用,它正在杀了我。

JAVASCRIPT

$(function() {
        $("input.boundChild[type=checkbox]").click(function() {              
            $(this).closest("input.boundParent").prop("checked", "checked");
        });
 });

HTML

<ul>
   <li class="collapsed expanded">
       <input id="0" class="boundParent" type="checkbox" >
        Australia
             <ul style="display: block;">
               <li>
                  <input id="0/" class="boundChild" type="checkbox" checked="">
                  <input type="hidden" value="Intel Australia PTY Ltd.">
               </li>
             </ul>
   </li>
</ul>

2 个答案:

答案 0 :(得分:9)

input个元素不能有子元素,在您的标记中input.boundParent不是input.boundChild的父母之一。它是ul元素的兄弟。您可以使用prev方法。

$(function() {
    $("input.boundChild[type=checkbox]").click(function() {              
        $(this).closest('ul').prev().prop("checked", this.checked);
        // $('input.boundParent').prop("checked", this.checked);
    });
});

http://jsfiddle.net/dhVvN/

答案 1 :(得分:0)

尝试:

$(this).parent("li.expanded").find("input.boundParent").attr("checked","checked");

如果您选择孩子时父母李总是拥有expanded课程。

相关问题