2016-10-25 1 views
0

바인딩 용 Knockout js를 사용하고 있습니다. 자식 요소 ID를 가져 오려고합니다. 확인란을 선택하면 고유 ID로 고유 ID가 생성됩니다.확인란을 선택한 특정 하위 요소 ID 가져 오기

//this div is inside the for each loop 

<div data-bind="attr: { id: 'unique_' + $index()},fav: ({item:someItem})" class="Tab"> 

// i need to get this itemIDs when i selecte the check box 
    <div id=randon_item1><div> 
    <div id=random_item2><div> 
    <div id=random_item3><div> 
    <div id=random_item4><div> 
    </div> 
    <input id="ckh1" type="checkbox" data-bind="value:userId(), click:  $root.toggleAssociation" /> 


//toggleAssociation// 
self.toggleAssociation = function (item, event) { 
here i am getting the item and all parent div with unique_0 ID which will be incremented for others div 
how i can get child element ID which is generated randomly 
} 

답변

0

knockout.js에 익숙하지 않지만 자식 요소 id 속성에 약간의 오타가있는 것으로 나타나며 부모 div에 다음과 같이 "Tab"클래스가 포함될 수 있습니다 :

<div data-bind="attr: { id: 'unique_' + $index()}, fav: ({item:someItem})" class="Tab"> 

    <div id="random_item1"></div> 
    <div id="random_item2"></div> 
    <div id="random_item3"></div> 
    <div id="random_item3"></div> 

    <input id="chk1" type="checkbox" data-bind="value:userId(), click: $root.toggleAssociation" /> 

</div> 

다음 기능 :

,210
self.toggleAssociation = function (item, event) { 
    alert(event.target.parentNode.id); 
} 

또는 jQuery를 : 당신이 그 "random_item"아이디의 각을 원하는 경우

self.toggleAssociation = function (item, event) { 
    alert($(event).parent().attr(id)); 
} 

당신이 당신의 self.toggleAssociation 기능에 그들을 반복 할 수는 :

$(event).siblings('div').each(function(){ 
    alert($(this).attr('id')); 
}); 
+0

예, 그 수정 , 나는 모든 것을 다시 쓰고 그것들을 놓쳤습니다. 감사. – LegenJerry

+0

@ 시간 주셔서 감사합니다],이게 내 문제를 해결했습니다. – Nikil

관련 문제