在這段代碼中,我只能輸入一個。如果列Term4
的任何一行有任何更改,則Ave
的值爲空白。如何使其動態?我需要幫助,因爲我所有的代碼都是一次一個地搜索,直到我想出了這個腳本。我被困在這裏好幾天了。任何幫助將是一個祝福。這是我第一次輸入...與javascript相同的文本字段輸入多次
樣品臺,輸出1:
Subject | Term1 | Term2 | Term3 | Term4 | Ave
Math 81 87 81 80 82.4
Science 89 83 81 80 83.25
如果我改變輸入term 4
,該ave
欄將顯示爲空白。
樣品臺輸出2:
Subject | Term1 | Term2 | Term3 | Term4 | Ave
Math 81 87 81 85
Science 89 83 81 80 83.25
HTML:
<tr>
<th colspan="3">Learning Areas</th>
<th colspan="2">Term 1</th>
<th colspan="2">Term 2</th>
<th colspan="2">Term 3</th>
<th colspan="2">Term 4</th>
<th>Ave</th>
</tr>
</thead>
<tbody>
@foreach($card['AllGrade'] as $subject)
{!! Form::hidden('grade_id[]',$subject['grade_id']) !!}
<tr>
<td colspan="3">{!! $subject->subject !!}</td>
<td colspan="2">{!! $subject->term_1 !!}</td>
<td colspan="2">{!! $subject->term_2 !!}</td>
<td colspan="2">{!! $subject->term_3 !!}</td>
<td colspan="2">{!! Form::text('term_4[]',$subject->term_4,['class'=>'form-control','name'=>'term_4','id'=>'term_4','value'=>'']) !!}</td>
<td colspan="2" class="aver" name ="ave" id ="ave" value=""> total</td>
</tr>
@endforeach
//javascript
<script type="text/javascript">
$("tbody tr").each(function() {
var total = 0;
var ave = 0;
var count = 1;
$(this).children('td').not(':first').not(':last').each(function() {
//"this" is the current element in the loop
var number = ($(this).children('input').length == 0) ? $(this).html() : $(this).children('input').first().val();
total += parseInt(number);
ave = total/count;
count++;
});
if('.form-control'){
$(this).on('keyup', 'td:eq(4) input', function(){
$('.form-control').on("input", function() {
var dInput = this.value;
total += parseInt(dInput);
ave = total/count-1;
});
console.log(ave);
$(this).parent().next().html(ave);
});
}
$(this).children('td').last().html(ave);
對不起,我無法編輯。注意:示例表輸出2:在Term4後值85,Ave值爲空。那是我做了我的變化。第二次,我對輸入進行了修改,沒有發生在Ave列上。 – Chee
我正要編輯帖子,但張貼後沒有編輯按鈕。這應該是我想要的視圖。謝謝你,先生。 – Chee
我真的不明白你在問什麼,但我可以告訴你,if('。form-control')'將永遠是真的(因爲if('任何非空字符串')總是true),並且從其他事件處理程序(即,嵌套的'.on()'調用)中添加事件處理程序通常不是解決大多數問題的正確方法。 – nnnnnn