繁体   English   中英

jQuery帮助同时选择父母和子女,但不帮助祖先的兄弟姐妹

[英]jquery help selecting parents and children at the same time, but not siblings of ancestors

我正在尝试编写一个jQuery调用,以在用户选中复选框ul菜单时选择“儿童”和“父母”。

这个jquery正确地选择了父母和孩子,但是在遍历父母时也选择了兄弟姐妹。

//Check the Parents
$(this).parents().find("input").checkUncheck(true);


//Check the Children
$(this).parent().siblings("ul").find("input").checkUncheck(true);

我不希望它在遍历祖先以选择“父母”时选择兄弟姐妹,但我不知道如何进行jquery。 如果有人对如何操作有一些提示,我将不胜感激!

谢谢。

以下是HTML:

<ul id="p_menu_nav">
    <li>    
        <span>
            <input type="checkbox" title="no"> 
            <img images/minus.gif" class="a_hand"> Wells Fargo
        </span>
        <ul >
            <li >
                <span>
                    <input type="checkbox" title="no"> 
                    <img src="images/plus.gif" class="a_hand"> Southern Utah
                </span>
            </li>
            <li >
                <span>
                    <input type="checkbox" title="no"> 
                    <img src="images/plus.gif" class="a_hand"> Northern Utah
                </span>
            </li>
            <li >
                <span>
                    <input type="checkbox" title="no"> 
                    <img images/minus.gif" class="a_hand"> Central Utah
                </span> 
                <ul >
                    <li>
                        <input type="checkbox" name="approved_property_id_list" id="4835">Apartment 1
                    </li>
                    <li>
                        <input type="checkbox" name="approved_property_id_list" id="4844">Apartment 10
                    </li>
                    <li>
                        <input type="checkbox" name="approved_property_id_list" id="4934">Apartment 100
                    </li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

尝试这样的事情:

    $(this).parents('li').find("> span > input").checkUncheck(true);

    //Check the Children
    $(this).closest('li').find('input').checkUncheck(true);

JSFiddle示例

尝试这个 :)

$(function(){
    $("#p_menu_nav input").click(function(){
        var that = "#"+this.id;
        $("#p_menu_nav li:has(img)").each(function(){
            if($(this).contents().find(that).length > 0){
                $(this).find(">span input").attr("checked",true);
            }
        });
    });
});
  1. 点击功能-仅用于测试
  2. 记住clicked元素的ID
  3. 选择所有分支(我假设所有分支都有img,但也可能是跨度)
  4. 检查是否在该分支中
  5. 如果是,则选中复选框(您已使用过功能checkUncheck(true))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM