这是一个与我之前发布的问题相关的问题......我正在尝试对所有以“pull”开头的输入元素求和,并将该总数放在“totalpull”输入字段中。我有那部分工作。现在,如果用户在“totalpull”输入字段中手动输入内容并将每个“拉”输入设置为该值,我正在尝试计算平均值。在下面尝试,但它不起作用......
//This is the sum formula, which works
$('input[name^=pull]').bind('keyup', function() {
$('#totalpull').val( $('input[name^=pull]').sumValues() );
});
$.fn.sumValues = function() {
var sum = 0;
$("input[name^='pull']").each(function() {
if ( $(this).is(':input') ) {
var val = $(this).val();
} else {
var val = $(this).text();
}
sum += parseFloat( ('0' + val).replace(/[^0-9-\.]/g, ''), 10 );
});
return sum;
};
//This is the avg formula, which does not work
//Keep getting v / rowCount.replace is not a function
$('input[name=totalpull]').bind('keyup', function() {
$('input[name^=pull]').each(function() {
$(this).val( $('input[name=totalpull').avgValues() );
});
});
$.fn.avgValues = function() {
var avg = 0;
var v = $("input[name=totalpull").val();
var rowCount = $("#convertRow tr").length;
avg += parseFloat( (v / rowCount).replace(/[^0-9-\.]/g, ''), 10);
return avg;
}
<table id="convert">
<tbody>
<tr><td><input type="text" value="" name="pull0" /></td></tr>
<tr><td><input type="text" value="" name="pull1" /></td></tr>
<tr><td><input type="text" value="" name="pull2" /></td></tr>
<tr><td><input type="text" value="" name="pull3" /></td></tr>
</tbody>
<tfoot>
<input type="text" id="totalpull" name="totalpull" value="" />
</tfoot>
</table>