简体   繁体   English

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

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

I'm trying to write a jquery call to select the "children" and the "parents" of a checkbox ul menu when the user checks the box. 我正在尝试编写一个jQuery调用,以在用户选中复选框ul菜单时选择“儿童”和“父母”。

This jquery selects the parents and the children properly, but it's also selecting the siblings when traversing up through the parents. 这个jquery正确地选择了父母和孩子,但是在遍历父母时也选择了兄弟姐妹。

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


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

I don't want it to select the siblings when traversing the ancestors to select the "parents", but I don't know how to do the jquery. 我不希望它在遍历祖先以选择“父母”时选择兄弟姐妹,但我不知道如何进行jquery。 If anyone has some tips on how to do this I woudld be grateful! 如果有人对如何操作有一些提示,我将不胜感激!

Thanks. 谢谢。

Heres the HTML: 以下是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>

Try something like this: 尝试这样的事情:

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

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

JSFiddle Example JSFiddle示例

Try this :) 尝试这个 :)

$(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. Click function - just for testing 点击功能-仅用于测试
  2. Remember the id of clicked element 记住clicked元素的ID
  3. Selecting all branches (I assumed all your branches has img, but it could be also span) 选择所有分支(我假设所有分支都有img,但也可能是跨度)
  4. Checking if in clicked is in this branch 检查是否在该分支中
  5. If YES then checking checkbox (You have used function checkUncheck(true)) 如果是,则选中复选框(您已使用过功能checkUncheck(true))

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

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