检查属性的所有元素并修改另一个元素

时间:2012-08-07 15:49:16

标签: javascript jquery

这就是我的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">

现在我的问题是我需要检查具有类child-of-id的元素是否具有selected-group =“1”属性,并且如果所有id-child-id都具有该属性,则添加新属性(选中,true)指向具有该特定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"

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

3 个答案:

答案 0 :(得分: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将为false。

更一般地说,我建议对非标准HTML属性使用data-属性。这样你的标记仍然有效。

答案 1 :(得分:0)

jQuery return true中的

.each与for循环中的continue;相同。 return false相当于break;。这些都不会用作迭代的返回值,而是传递给allSelected。您可能正在查看类似.filter而不是.each的内容,这是一个reduce,其中返回false意味着该元素将从列表中排除。

这样的事情可能会起到作用:

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

答案 2 :(得分: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);
    }
});
相关问题