これは、以前に投稿した質問に関連するものです...「pull」で始まるすべての入力要素を合計し、その合計を「totalpull」入力フィールドに入れようとしています。私はその部分を機能させています。現在、ユーザーが「totalpull」入力フィールドに手動で何かを入力し、各「pull」入力をその値に設定した場合の平均を計算しようとしています。以下を試してみましたが、うまくいきません...
//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>