私は学校カード用のテーブルを持っています。目標は平均グレーディング期間を表示することです。平均結果はデータベースからのもので、平均結果はjavascriptからのものです。私の現在のコードでは、エラーを生成します。e.text()は関数ではありません.Basically sum変数は、parseInt.Anyoneから値をキャッチしませんでした。この問題を解決するアイデアはありますか?javascriptの入力を解析する
******サンプルの結果******
Subject | Col1 |Col2 |Col3 |Col4
1 80 80 86 80 (80+80+86+80)/4 Note: not this way
2 86 85 86 81
3 80 87 85 86
Result 82 84 and so on..
It should be:
(80+86+80)/3 number of rows
view.blade.php
<tr>
<th colspan="3">Subjects</th>
<th colspan="2">First Grading</th>
<th colspan="2">Second Grading</th>
<th colspan="2">Third Grading</th>
<th colspan="2">Fourth Grading</th>
</tr>
</thead>
<tbody>
@foreach($update_card['AllGrade'] as $subject)
{!! Form::hidden('grade_id[]',$subject['grade_id']) !!}
<tr>
<td colspan="3">{!! $subject->subject !!}</td>
<td colspan="2"><input type="text" name="term_1[]" value="{!! $subject->term_1 !!}" class="form-control number-only"></td>
<td colspan="2"><input type="text" name="term_2[]" value="{!! $subject->term_2 !!}" class="form-control number-only"></td>
<td colspan="2"><input type="text" name="term_3[]" value="{!! $subject->term_3 !!}" class="form-control number-only"></td>
<td colspan="2"><input type="text" name="term_4[]" value="{!! $subject->term_4 !!}" class="form-control number-only"></td>
<td class="ave" value="0"></td>
</tr>
@endforeach
<tr id="average">
<td colspan="3">Average</td><td colspan="2">0</td> <td colspan="2">0</td> <td colspan="2">0</td> <td colspan="2">0</td>
</tr>
</tbody>
ここではJavaScript
$("input").on("keyup", function() {
$("tbody tr").each(function(index) {
var sum = 0; // The summation of all terms.
var card = 0; // Number of cells.
$(this).children('td').not(':first').each(function(i, e) {
card++;
sum+= parseInt(e.text().trim()); //this is the error
});
var avg = sum/card;
console.log(avg);
$("#average td:nth-of-type("+index+")").html(avg);
});
});
チー、あなたは、TDのテキストから値を取っているが、あなたは内部のtdのあるファイルを入力に常駐を重視するので、ここにあなたが何をすべきかです: 合計+ =のparseInt($(E) .find( "input")。val()); //これはエラー – suman
sum1が言う、または '$(this).children( 'input')...' – Octopus
私はデバッグしようとしましたが、別の問題はその列に応じて計算していないということです。 First gradingの下にすべての値を追加してから、残りのグレーディングと合計と平均を取得します。 – Chee