2011-08-12 2 views
1

그래서 나는 다음과 같은 구조를 가지고 :ID를 가지고 형제를 얻을 수

<div id="some id"> 
    <div> 
      <input id="some other id" ....> 
      .....bunch of other inputs with no id field.... 
      <select onclick="findSiblingWithId()">    
    </div> 
    <div> 
      <input id="some other id" ....> 
      .....bunch of other inputs with no id field.... 
      <select onclick="findSiblingWithId()">    
    </div> 
    <div> 
      <input id="some other id" ....> 
      .....bunch of other inputs with no id field.... 
      <select onclick="findSiblingWithId()">    
    </div> 

그래서 내 findSiblingWithId에서 내가 찾고 있어요 ID를 모르는를, 내가 아는 모든는 그의 다른 구성 요소 같은 div, 오직 하나는 id를가집니다. 어떻게해야합니까?

편집 : 내가 충분히 잘 설명하지 못했기 때문에 더 많은 정보를 추가하려고합니다. 예를 들어 select component가 변경되었을 때 (예를 들어, 버튼 또는 아무것도 될 수 있지만, 중요하지는 않습니다) 동일한 div에서 해당 <input id="some other id" ..>을 얻고 싶습니다.

위의 설명을 시도한 것처럼 하나의 거대한 div 인 <div id="some id">이 있습니다. 이제 작은 숫자 인 <div>이 포함되어 있습니다.

각각의 작은 div에는 ID가없는 입력란 하나와 ID가없는 기타 입력란이 있습니다. 이제 버튼을 누르거나 아무거나, 같은 div에있는 구성 요소 (올바른 작업은 형제가 될 것이라고 생각 했음)가 동작하는 경우 중요하지 않습니다. ID 속성이있는 입력을 찾을 수있는 방법이 있습니다. ?

감사합니다, 보그

+0

? 이 기능을 사용하여 무엇을 얻고 싶습니까? 코드를 보여줄 수 있습니까? – SergeS

+0

다음 입력 필드 또는 다음 div를 가져 오시겠습니까? 둘 다 가능한 모든 형제를 통해 루프, 또는 간단한 indexOf 검색도 가능합니다. –

+0

여기에 당신이 찾고있는 것에 관한 게시물이 있습니다 ... http://stackoverflow.com/questions/842336/is-there-a-way-to-select-sibling-nodes –

답변

3

부모의 모든 자식을 반복 할 수 있습니다. :

function findSiblingWithId(element) { 
    var siblings = element.parentNode.children, 
     sibWithId = null; 
    for(var i = siblings.length; i--;) { 
     if(siblings[i].id) { 
      sibWithId = siblings[i]; 
      break; 
     } 
    } 

    if(sibWithId) [ 
     // found it 
    } 
}; 

당신은 (당신이 적절한 select 필드가 희망) 클릭 한 요소를 통과해야; 당신은 사용자가 값을 선택할 때 트리거 갖고 싶어

<select onclick="findSiblingWithId(this)">  

change 이벤트에 대한 이벤트 처리기를 부착하는 것이 좋습니다.

+0

감사합니다. 이와 같은 것이 저에게 효과적이었습니다. – Bogdan

0

재귀 적 부모 DIV를 통해 반복하고있는 입력을 분리하는 논리를 추가 ID를 jQuery를 사용

0
function findSiblingWithId(element) { 
var sibling = null; 

if (element.parentNode != null) { 
    sibling = element.parentNode.nextSibling; 
    // Then go through the child elements of your sibling node and get the one with an id 
} 

}

+0

이것은 부모의 다음 형제. –

+0

네,하지만 그 뒤에 그는 ID가 들어간 입력을 찾을 수 있습니다. – Dorpsidioot

0

:

<!--Modify HTML so input is --> 
<select onclick="findSiblingWithId(this)"> 

findSiblingWithId(elem){ 
    return $(elem).siblings("[id]"); //if you need all 
    //return $(elem).siblings("[id]:eq(0)"); //if you need only first 
} 
관련 문제