2013-02-14 4 views
0

동적 테이블 행 집합을 반복합니다. jquery를 사용하여 각 테이블 행에서 cell2 값을 캡처하려고하는데, 매번 cell2 row1 값을 반환합니다. 때마다 셀 1 올바른 행 값을 인식, 아무도 내게 잘못 가고 있다고 말할 수 있습니까? 아래 샘플 코드를 참조하십시오.jquery의 각 테이블 행에 대한 텍스트 상자를 계산합니다.

도움을 주셔서 감사합니다.

//html 
<table class=plantTable> 
<tr> 
<td><input type=text value=0 class=cell1></td> 
<td><input type=text value=0 class=cell2></td> 
<td><input type=text value=0 class=cell3></td> 
</tr> 
<tr> 
<td><input type=text value=0 class=cell1></td> 
<td><input type=text value=0 class=cell2></td> 
<td><input type=text value=0 class=cell3></td> 
</tr> 
<tr> 
<td><input type=text value=0 class=cell1></td> 
<td><input type=text value=0 class=cell2></td> 
<td><input type=text value=0 class=cell3></td> 
</tr> 
</table> 

//jquery 
    tr = $(".plantTable tr").length; 

    $(tr).each(function() { 
     $(this).find($(".cell1").blur(function() { 
     cell1val = parseInt($(this).val()); 
     cell2val = parseInt($(".cell2").val()); //Returns the value of cell2 in the first row only? 
    })); 
+3

'tr' 정수,'TR = $ (". plantTable TR") 길이와 같다 .. 향후에 추가되는 경우에도,' –

+0

HTTP : //codepen.io/anon/pen/GCdpA – Shanimal

답변

0

내부에서 찾기를 다시하면 상단에서 문서를 볼 것이므로 row1 cell2를 찾습니다. 당신이 보이는대로 현재 항목의 형제를 원하는 경우,이 작업을 수행 : 현재 발견 된 CELL1 옆에

$("tr").each(function() { 
     $(this).find($(".cell1")).blur(function() { 
     cell1val = parseInt($(this).val()); 
     cell2val = parseInt($this.siblings(".cell2").val()); 
     }); 
    }); 

이는 CELL2의 값을 검색합니다. 솔루션에 대한

$('.plantTable tr').each(function() { 
    $(this).find('.cell1').blur(function() { 
     cell1val = parseInt($(this).val()); 
     cell2val = parseInt($(this).siblings(".cell2").val()); 
    }); 
}); 
+1

브래킷이 일치하지 않습니다. – BenM

+0

Eh 와우 ... 실제로 많은 것이 잘못되었습니다. 감사합니다 벤 ... 모든 복사 붙여 넣기. – Spork

+0

감사합니다. Spork, 위의 해답을 시도했지만 cell2 val이 NaN을 반환합니다. NaN은 숫자 값을 가지고 있기 때문에 이상합니다. – ullevi83

0

귀하의 jQuery를은 다음과 같이한다. . 코드가 잘 작동 아래 다른 입력 요소는

// If you want to maintain resusable collection of all inputs inside 
var inputCollection = $([]).pushStack($("table.plantTable :input")); 
// Then do operations with inputCollection 
// Else, you could follow chaining like below 

$("table.plantTable").delegate("input", "blur", function() { 
    var inputs = $(this).closest('tr').find(':input'); 
    // do whatever you want to with inputs 
}); 
+0

당신은 같은 일을했다 확신 : D를 $ (이) .find ($ ("CELL1.") 블러 (함수() { 대 $ (이) .find ($ ("CELL1.. ") 0 블러 (기능() { 또는 실제로 .find ('. 셀 1') ...... – Spork

+0

좋은 전화 - 그 싶었어! – BenM

+0

건배 BenM, 위의 솔루션을 시도했지만 여전히 선택 cell2의 첫 번째 행 값, 이상한. 이것을 다시 생각해 봐야합니다 . – ullevi83

0

가 완벽하게 일을하지만, 새로운 요소가 여기에 추가 될 때마다 내가 그들을 의심 :

관련 문제