[英]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
.each
与continue;
相同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.