在这种情况下,我真的推荐使用jQuery让你的生活变得更容易,尤其是因为你至少要做一个for循环,如果不是两个。
您需要遍历每一行,获取前面的11个值,然后进行计算。
在你的jsFiddle中,下面的代码应该可以工作。我已经相应修改您的jsfiddle:http://jsfiddle.net/SLC5F/1/
$('tr', '#order-table').each(function() {
var total = 0;
var $rows = $(this).prevAll();
// return if we don't have enough rows
// probably better to just add a class to the active rows instead,
// but it's not my job to format your code.
if ($rows.length < 13) return;
$rows = $rows.slice(0, 11);
// again, would be better to add a class to active inputs instead of using
// a not() filter. you can do this yourself.
total += parseFloat($(this).find('input').not('.row-total-input, [readonly]').val());
$rows.each(function() {
total += parseFloat($(this).find('input').not('.row-total-input').val());
});
// return if total isn't a number
if (isNaN(total)) return;
var monthly_average = total/12;
// again, would be better to add classes to these TDs instead of using eq()
// make sure you do this before using this script in a live application.
$('td', this).eq(2).text(total);
$('td', this).eq(3).text(Math.round(monthly_average * 100)/100);
$('td', this).eq(4).text(Math.round((monthly_average - 115.763) * 100/115.764 * 100)/100);
});
谢谢你的答复,并转换成JavaScript的JQuery的,现在我几乎是在和平的评论是好的,我改变这些东西,如果我想采取App.CA的最后6个值的平均值..我可以包括在相同的功能,或者我必须写一个单独的或在相同的我可以包括... –