简体   繁体   English

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

[英]Check all elements for an attribute and modify another element

This is how my html looks, 这就是我的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">

now what is my problem is that I need to check if the element with class child-of-id have property selected-group="1" and if all of the child-of-id have the property then add new property(checked,true) to element with that particular id. 现在我的问题是,我需要检查类为child-of-id的元素是否具有属性selected-group =“ 1”,并且是否所有的child-of-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);
            }
      });

That means by the end my result should be something like: 那意味着最后我的结果应该是这样的:

 <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">

but element with id = "group-1-11" should not have that attribute checked = "true" id = "group-1-11"元素不应将该属性checked = "true"

I hope the context was clear. 我希望背景清楚。 I probably have a bug in the script, hence result output is not as expected. 我的脚本中可能有一个错误,因此结果输出与预期不符。 Please help me fix the bug, I was expecting allSelected to be boolean but I probably am not very familiary with the methodology. 请帮助我修复错误,我希望allSelected为布尔值,但是我可能不太熟悉该方法。

.each() does not function as you want it to. .each()不能按您期望的方式运行。 You need to create a variable before your call to .each() , and then modify the variable if you encounter the condition you're looking for. 您需要在调用.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);
    }
});

This code will loop through all .child-of-(parentID) elements. 此代码将遍历所有.child-of-(parentID)元素。 If any of them do not have selected-group="1" , then allSelected will be false when the loop completes. 如果其中任何一个都不具有selected-group="1" ,则在循环完成时allSelected将为false。

On a more general note, I would recommend using data- attributes for non-standard HTML attributes. 更笼统地说,我建议对非标准HTML属性使用data- -attribute。 This way your markup remains valid. 这样您的标记仍然有效。

return true in jQuery .each is the same thing as continue; 在jQuery中return true .eachcontinue;相同continue; in a for loop. 在for循环中。 return false is the equivalent of break; return false等于break; . None of these will be used as return values for the iteration, and passed to allSelected . 这些都不用作迭代的返回值,并传递给allSelected You might be looking at something like .filter rather than .each , which is a reduce, where returning false would mean that the element is excluded from the list. 您可能正在看.filter而不是.each类的东西,这是一个简化,其中返回false意味着该元素将从列表中排除。

Something like this might do the trick: 这样的事情可能会解决问题:

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

You can try something like: 您可以尝试如下操作:

$.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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 取得动态元素属性并在另一个元素中使用 - Taking a dynamic elements attribute and using it in another element 检查一个元素是否具有特定值,然后修改另一个元素 - Check if an element has a specific value, then modify another element 检查一个javascript数组是否包含另一个数组的所有元素或元素值的一部分 - Check if a javascript array contains all elements or part of element values of another array 检查数组是否包含另一个数组的所有元素 - Check if array contains all elements of another array 检查数组中的所有元素是否都存在于另一个数组中 - Check if all elements in an array are present in another 当有多个元素时,单击另一个元素时更改元素的属性 - Change attribute of element when another element is clicked, when there are multiple elements CKEditor:检查元素路径是否包含具有特殊属性和值的元素 - CKEditor: Check if elements path contains element with special attribute and value 如何检查一个数组是否包含另一个数组的所有元素 - How to check if an array contains all the elements of another another array 使用html elements属性来查找和更改另一个html元素 - Using html elements attribute to locate and alter another html element 选择具有设置为特定值的属性的元素内的所有元素 - Select all the elements within an element having an attribute set to a specific value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM