2010-08-17 6 views
2

누구든지 나를 도울 수 있는지 궁금합니다.여러 텍스트 상자를 추가하는 jQuery

일부 텍스트 상자 (동적 양, 3 일 수 있으며 30 일 수 있음)가있는 웹 페이지가 있으며 각 사용자는 숫자를 입력 할 수 있으며 하단에 총 개수가 있습니다.

$('input.qty').change(function() { 
// Get the current total value 
var total = $('#total'); 
// Update it with the changed input's value 
total.val(parseInt(total.val()) + parseInt($(this).val())); 
}); 

예 HTML : 현재

나는이가 잘 처음 추가하지만 당신이 값을 변경 시작할 때 단순히보다는 전체에 새로운 금액을 추가

<td><input type="text" class="qty" value="0" /></td> 
<td><input type="text" class="qty" value="0" /></td> 
<td><input type="text" class="qty" value="0" /></td> 
<td><input type="text" id="total" value="0" /></td> 

모든 상자를 추가합니다.

어떻게 정렬 할 수 있습니까? 아마 각 attr을 어떻게 든 사용합니까?

답변

2

이처럼, 그들 모두를 통해 루프 A .each()를 사용하고이를 요약 할 수 있습니다

$('input.qty').change(function() { 
    var sum = 0; 
    $('input.qty').each(function() { sum += parseInt(this.value, 10); }); 
    $('#total').val(sum); 
}); 

You can give it a try here을, 당신은 모든 키를 누를에 CALC하려면 다음 .keyup() 대신 .change(), like this를 사용합니다.

관련 문제