jquery有助于同时选择父母和子女,但不能选择祖先的兄弟姐妹
我试图编写一个jquery调用,当用户选中复选框时,选择复选框ul菜单的“子项”和“父项” 这个jquery正确地选择了父对象和子对象,但在向上遍历父对象时也选择了同级对象jquery有助于同时选择父母和子女,但不能选择祖先的兄弟姐妹,jquery,Jquery,我试图编写一个jquery调用,当用户选中复选框时,选择复选框ul菜单的“子项”和“父项” 这个jquery正确地选择了父对象和子对象,但在向上遍历父对象时也选择了同级对象 //Check the Parents $(this).parents().find("input").checkUncheck(true); //Check the Children $(this).parent().siblings("ul").find("input").checkUncheck(true); 我
//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);
试试这个:)
- 单击函数-仅用于测试
- 记住单击的元素的id
- 选择所有分支(我假设所有分支都有img,但也可以是span)
- 正在检查此分支中是否已单击“入”
- 如果是,则选中复选框(您已使用函数checkUncheck(true))
您能否通过解释什么应该被选中/取消选中以及何时选择哪个复选框来澄清?这个问题我已经读了五遍了,但似乎都不明白(也许只有我:)
$(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);
}
});
});
});