2012-08-01 5 views
49

열이있는 일련의 행을 가지고 있으며 input 필드 (가격 입력)에 이전 열에있는 input 필드의 값을 선택하고 싶습니다. 키가 해제됩니다.Jquery가 가장 일치하는 요소를 찾습니다.

이 나는 ​​시도했다 :

quantity = $(this).parent().parent().children().val() ; 
quantity = $(this).parent().parent().children().closest('.inputQty', this).val() ; 

그러나도 작동합니다.

는 DOM의 예 :

<div class="row"> 
    <div class="column"><input class="inputQty" id="quantity0" /></div> 
    <div class="column"><input class="someOther" id="Other0" /></div> 
    <div class="column"> 
     <div class="cSelect"> 
      <select id="currency0"><option>£</option></select> 
      <input class="price" id="price0" /> 
     </div> 
    </div> 
</div> 

답변

104
var otherInput = $(this).closest('.row').find('.inputQty'); 

입니다.

+0

이 더 있는가 현재 사용중인 요소와 동일한 수준에서 무언가를 찾는 다른 함수? 당신은 언제나 위로 올라 가야합니까? – Majo0od

+0

이 질문은 같은 수준에서 뭔가를 찾는 것에 관한 것이 아니 었습니다. 다시 올라가는 것이 필요했습니다. 그러나 그렇습니다. 같은 레벨에 대해 .siblings()과 다양한 next()와 prev ~()'메소드를 호출합니다. – Utkanos

+2

덕분에 나는 그런 해결책을 찾고 있었고 그것은 나를 위해 일했습니다! 하지만 가장 중요한 것은 closest() 함수에 대해 더 많이 배웠다. 정말 고맙습니다! Davide, –

1

에서, this 요소의 .column 부모를 받기 이전 형제를 얻을, 다음이 모든 입력을 찾을 :

$(this).closest(".column").prev().find("input:first").val(); 

데모 : http://jsfiddle.net/aWhtP/

0

시도해 볼 수 있습니다.

$(this).closest(".column").prev().find(".inputQty").val();

10

closest()는 부모를 찾습니다, 난 당신이 정말 원하는 것을 추측하고있어 다시 다음, 행 레벨까지 간다 .find()

$(this).closest('.row').children('.column').find('.inputQty').val(); 
관련 문제