2012-08-07 79 views
0

这是我的HTML的外观,检查属性的所有元素,并修改另一个元素

<tr id="group-1-11"> 
<tr class="child-of-group-1-11 " selected-group="1" > 
<tr id="group-1-12" class="child-of-group-1-11" parent-id="group-1-11" selected-group="1"> 
<tr class="child-of-group-1-12" parent-id="group-1-12" selected-group="1"> 
<tr class="child-of-group-1-12" parent-id="group-1-12" selected-group="1"> 
<tr class="child-of-group-1-11" selected-group="1" > 
<tr id="group-1-85" class="child-of-group-1-11" parent-id="group-1-11" > 
<tr class="child-of-group-1-85" selected-group="1" style="display: none;"> 
<tr id="group-1-355" class="child-of-group-1-85" parent-id="group-1-85" selected-group="1"> 
<tr id="group-1-2" class="child-of-group-1-11 parent " parent-id="group-1-11"> 

什么现在是我的问题是,我需要检查是否带班儿童的-ID的元素有属性selected-group =“1”,如果所有的孩子的id有属性,然后添加新的属性(选中,真)元素与该特定的ID。

// I have an array of ids 
// var uniqueParentArray = ['group-1-11', 'group-1-12', 'group-1-85',...]; 
$.each(uniqueParentArray, function(index, parentId) { 
      var allSelected = $('.child-of-'+parentId).each(function(){ 
       var selectedGroup = $(this).attr('selected-group'); 
       if(selectedGroup != '1') 
        return false; 
       return true; 
      }); 
      if(allSelected) { 
       $('#'+parentId).attr("checked", true); 
      } 
     }); 

由最终意味着我的结果应该是这样的:

<tr id="group-1-12" class="child-of-group-1-11" parent-id="group-1-11" selected-group="1" checked="true"> 
<tr id="group-1-85" class="child-of-group-1-11" parent-id="group-1-11" checked="true"> 

但与元素id = "group-1-11"不应该有属性checked = "true"

我希望背景是清楚的。我可能在脚本中有一个错误,因此结果输出不像预期的那样。请帮我修复这个错误,我期待allSelected是布尔值,但我可能不太熟悉这个方法。

回答

0

.each()不能按照您的要求工作。您需要在致电.each()之前创建一个变量,然后在遇到您要查找的条件时修改该变量。

$.each(uniqueParentArray, function(index, parentId) { 
    var allSelected = true; 
    $('.child-of-'+parentId).each(function(){ 
     var selectedGroup = $(this).attr('selected-group'); 
     if(selectedGroup != '1') { 
      allSelected = false; 
     } 
    }); 
    if(allSelected) { 
     $('#'+parentId).attr("checked", true); 
    } 
}); 

此代码将遍历所有.child-of-(parentID)元素。如果它们中的任何一个没有selected-group="1",则在循环完成时allSelected将为假。

在更一般的说明中,我建议使用data-属性来表示非标准HTML属性。这样你的标记仍然有效。

+0

嗯!为什么我没有想到它,谢谢@jackwanders。但这确实需要不必要的循环,再次感谢你,但我想我还会等待其他解决方案。 – 2012-08-07 16:01:27

0

return true in jQuery .each与for循环中的continue;是一样的。 return false相当于break;。这些都不会被用作迭代的返回值,并传递到allSelected。您可能会看到类似.filter而不是.each,这是一个减少,其中返回false将意味着元素从列表中排除。

像这样的事情可能做的伎俩:

$('#' + uniqueParentArray.join(', #')).attr('checked', function() { 
    return $('.child-of-' + $(this).attr('id')).filter(function() { 
     return $(this).attr('selected-group') != '1'; 
    }).length == 0; 
}); 
0

你可以尝试这样的:

$.each(uniqueParentArray, function(index, parentId) { 
    // retrieve not selected elements for the given parent 
    var notSelectedElements = $('.child-of-'+parentId+'[selected-group!="1"]'); 
    // if all elements are selected 
    if(notSelectedElements.length === 0) { 
     $('#'+parentId).attr("checked", true); 
    } 
}); 
相关问题