2013-04-02 2 views
0

현재받은 할인을 추가하는 기능이 있습니다. 각 입력의 평균 개로 변경하고 싶습니다.javascript의 평균 입력

function calculateAverageDiscount() { 
    var avediscount = 0; 
    $("table.authors-list").find('input[name^="discount"]').each(function() { 
     avediscount += +$(this).val(); 
    }); 
    $("#avediscount").text(avediscount.toFixed(2)); 
} 

감사합니다.

답변

2

첫 번째 요소의 목록을 가져 오기 :

var $disc = $("table.authors-list").find('input[name^="discount"]'); 

을 다음 길이 취

var n = $disc.length; 

을하고있는 그대로 다음 합계를 가지고 있지만, 을하도록 이전에 얻은 목록을 사용하여 자신을 반복하지 마십시오.

$disc.each(function() { 
    ... 
}); 

나머지는 명백해야한다 ... ;-)

+0

감사합니다. Alnitak, – Smudger

1

당신은 요소의 수를 얻고 그 번호로 총액을 분할해야합니다.

var avediscount = 0; 
var length = $("table.authors-list").find('input[name^="discount"]').each(function() { 
    avediscount += +$(this).val(); 
}).length; 
$("#avediscount").text(avediscount.toFixed(2)/length); 
+1

감사합니다 vdua, 작품 100 %! – Smudger