2012-08-07 2 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"이고 모든 child-of-id가 속성을 갖는 경우 해당 특정 id를 가진 요소에 새 속성 (checked, true)을 추가합니다. 내 결과가 같은 것을해야 말을 의미

// 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); 
      } 
     }); 

: id = "group-1-11"해야하지

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

하지만 요소는 그 속성 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이 false가됩니다.

비표준 HTML 속성에 대해 data- 속성을 사용하는 것이 좋습니다. 이렇게하면 마크 업이 유효합니다.

+0

흠을! 왜 내가 그것에 대해 생각하지 않았어, 고맙다. 하지만 이것은 불필요한 루프를 요구합니다. 다시 한번 감사드립니다.하지만 나는 다른 해결책도 기다릴 것이라고 생각합니다. –

0

return true in jQuery .each은 for 루프에서 continue;과 동일한 것입니다. return falsebreak;과 같습니다. 이들 중 어느 것도 반복에 대한 반환 값으로 사용되지 않고 allSelected으로 전달됩니다. .each이 아닌 .filter과 같은 것을 보았을 수 있습니다. 줄이면 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); 
    } 
});