[英]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);
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);
}
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.