2017-04-21 67 views
-1

在這段代碼中,我只能輸入一個。如果列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); 
+0

對不起,我無法編輯。注意:示例表輸出2:在Term4後值85,Ave值爲空。那是我做了我的變化。第二次,我對輸入進行了修改,沒有發生在Ave列上。 – Chee

+0

我正要編輯帖子,但張貼後沒有編輯按鈕。這應該是我想要的視圖。謝謝你,先生。 – Chee

+0

我真的不明白你在問什麼,但我可以告訴你,if('。form-control')'將永遠是真的(因爲if('任何非空字符串')總是true),並且從其他事件處理程序(即,嵌套的'.on()'調用)中添加事件處理程序通常不是解決大多數問題的正確方法。 – nnnnnn

回答

0

爲了避免楠問題,我確信,改變的輸入被視爲0。然後重新計算平均,那麼它的工作。

  $("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++; 
       }); 


       $(this).children('td').last().html(ave); 


       if('.form-control'){ 
       $(this).on('keyup', 'td:eq(4) input', function(){ 
        $('.form-control').on("input", function() { 
         var dInput = parseInt(this.value); 
         if(!dInput || dInput === NaN) { 
          dInput = 0; 
         } 

        var total = 0; 

        var count = 1; 
        $(this).parent().siblings().not(':first').not(':last').each(function() { 

        //"this" is the current element in the loop 
        var number = $(this).html(); 
        total += parseInt(number); 
        count++; 

        }); 

        total += dInput; 
        console.log(total); 
        console.log(count); 
        var ave = total/count; 

        //console.log(ave); 
        $(this).parent().siblings(":last").html(ave); 

        calcTotalave(); 
        }); 

      }); 
      } 

    }); 
    calcTotalave(); 
    // calculate total average 
    function calcTotalave() { 
     var totalAve = 0; 
     $(".aver").each(function() { 
      console.log($(this).html()); 
      var thisAve = parseFloat($(this).html()); 
      if(!thisAve || thisAve === NaN) { 
       thisAve = 0; 
      }     

      totalAve += thisAve; 

     }); 
     console.log(totalAve); 
     $("#total-ave").val(totalAve); 
    } 
+0

計算是錯誤的,也許輸入和計數是錯誤的。我現在計算出來。 – Chee

+0

我得到適當的平均值。 –

+0

對不起,先生,這段代碼是正確的。它只是因爲我有近距離括號的不匹配。最後你是一個誰得到它的權利。我想投票這個答案,但我的地位是不允許的。謝謝你這麼多! – Chee

相關問題