2016-05-01 13 views
0

マイフォームで合計を計算する方法を enter image description here最後のTD入力

よりよく理解するために上の画像をご覧下さい。

私は各行の最後のtd(合計)の最初の2つのtdの合計を取得したいと思います。 iはTD印刷テーブルの(総)ID

function foc(id){ 
    $id=id; 
    $par=$(id).closest('tr').children(find('td:last')).children('input #total').val(); 
    console.log($par); 
} 

とPHPコードの値を取得しようとし、このコードに 現在のjQueryのコードは

for ($i =1 ; $i <=5; $i++) 
    { 
     $name=$i+1; 
     echo '<tr >'; 
     echo '<td><input type="hidden" value="10148301'. $i.'">10148301'. $i.'</td>'; 
      for ($j = 1; $j<=$count+1; $j++) 
       { 
        if ($j==$count+1) 
         { 
          $name="total[]"; 
          $class="total"; 
          $on=""; 
         } 
        else 
         { 
          $name="any"; 
          $on='onfocus="foc(this.id);"'; 
          $class="score"; 
         } 
        echo '<td>'; 
        echo '<input type="text" style="width:50px;" '.$on.' id="'.$class.'" class="form-control" name="'.$name.'">'; 
        echo '</td> '; 
      } 
         echo '</tr>'; 
+0

値を入力したとき、またはボタンがあるときに計算しますか? – DevNiels

+0

あなたの問題は何ですか? – Mohammad

+0

値を入力すると計算されたい –

答えて

0

一つのアプローチは、keyUpイベントイベントを結合するためにjQueryを使用することですテキスト入力に:

$('tr > td').not(':last-child').on('keyup', 'input[type="text"]', function() { 
    var parentRow = $(this).parents('tr'); 
    var q1Value = parentRow.find('td:eq(0) > input').val(); 
    var q2Value = parentRow.find('td:eq(1) > input').val(); 
    var totalInput = parentRow.find('td:last-child > input'); 
    if (q1Value.length && q2Value.length) { 
     totalInput.val(parseInt(q1Value, 10) + parseInt(q2Value, 10)); 
    } else { 
     totalInput.val(''); 
    } 
}); 

あなたは必要最小限の例CodePen hereを見つけることができます。

+0

thnx bro :)しかし、私は手動で書いたので、今は動作しています 'var tbl = $( '#fq'); tbl.find( 'tr')。each(function(){ var sum = 0; $(this).find( '#score')それぞれの(function(){ if(!isNaNです。値)&& this.value.length!= 0){ 合計+ = parseInt(this.value); } }); $(this).find( '#合計')。 }}); ' –

関連する問題