2011-03-28 43 views
0

我想写一个jquery调用来选择复选框ul菜单的“孩子”和“父母”,当用户选中框。jquery帮助同时选择父母和孩子,但不是祖先的兄弟姐妹

这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> 
+0

你可以通过解释究竟应该得到选中/取消和选择复选框时澄清?我已经读了五次这个问题,似乎无法得到它(也许这只是我:) – karim79 2011-03-28 23:26:24

回答

1

尝试是这样的:

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

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

JSFiddle Example

+0

awsome,谢谢...作品像一个魅力! – Ronedog 2011-03-29 02:41:18

0

试试这个:)

$(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. 记住点击的元素的ID
  3. 选择所有分支(我假设你所有的分支机构有张图片,但它可以同时跨越)
  4. 检查如果点击在该分支
  5. 如果是,那么检查复选框(已使用功能checkUncheck(真))