2017-10-04 20 views
0

javascriptとそのイベントの新機能です。ここで私は収入のタイトルとその価格を持っているテーブルのための私のHTMLコードです。 Javascript:keyupイベントの入力は合計を計算します

<table class="table table-hover table-bordered" id="incomeId"> 
    <thead> 
    <tr> 
     <th> sn </th> 
     <th> Title of income </th> 
     <th> Price </th> 
     <th> Action </th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <td>1</td> 
     <td> 
     <select name="name" id="inputID" class="form-control"> 
      <option value=""> -- Select One --</option>       
     </select> 
     </td> 
     <td> 
     <input type="text" name="name" id="income_price" class="form-control income" value="" title="" required="required"> 
     </td> 
     <td> 
     <span class="glyphicon glyphicon-trash"></span> 
     </td> 

    </tr> 
    </tbody> 
    <tfoot> 
    <tr> 
     <td></td> 
     <td></td> 
     <td> Total: <div class="total" id="total"></div> </td> 
     <td></td> 
    </tr> 
    </tfoot> 
</table> 

<button type="button" class="btn btn-info" id="add-new">Add New Income</button> 

は、コードがあるjavascriptのテーブルに新しい行を追加するには、

$('#add-new').on('click', function() { 
     $("#incomeId").each(function() { 

      var tds = '<tr>'; 

      jQuery.each($('tbody tr:last td', this), function() { 
       tds += '<td>' + $(this).html() + '</td>'; 
      }); 

      tds += '</tr>'; 
      if ($('tbody', this).length > 0) { 
       $('tbody', this).append(tds); 
      } else { 
       $(this).append(tds); 
      } 
     }); 
    }); 

しかし、私は、テーブルに新しい行を作成する前に、全セクションのそれぞれに、すべての価格セクションとの和から価格を取得したいです。新しい行を作成すると、最近作成された価格が前の合計価格に加算されます。 解決策を見つけてください。

+0

はどのようにあなたの質問は、タイトル内の複数のkeyUpイベントに関連していますか? –

+0

私はkeyupイベントを使用して各入力ボックスから値を取得し、合計セクションに追加したいと考えています。 –

答えて

1

したがって、updateTotalイベントハンドラにバインドする入力を動的に挿入していますか?

最も簡単なオプションは、イベントハンドラを静的なラッパー要素に単にバインドすることです。私はフォーム要素を提案しますが、あなたの例では1つもないので、テーブル本体は別のオプションかもしれません。

$('table#incomeId > tbody').on('keyup', 'input', function(e){ 

     var total = 
      $(e.delegateTarget) 
       .find('input') 
      .map(function(){ 
       return parseFloat($(this).val()) || 0; 
      }) 
      .get() 
      .reduce(function(a,b){ 
       return a + b; 
      }); 

     $('#total').text(total); 


    }); 

$('#add-new').on('click', function() { 
 
    $("#incomeId").each(function() { 
 
    
 
    var tr = $('tbody > tr:last', this).clone(); 
 
    tr.find('input').val(''); 
 
    var sntd = tr.find('td:first'); 
 
    var sn = parseInt(sntd.text()) + 1; 
 
    sntd.text(sn); 
 
    
 
    $('tbody', this).append(tr); 
 
    
 
    
 
    
 
    return; 
 

 
    }); 
 
}); 
 

 
$('table#incomeId > tbody').on('keyup', 'input', function(e) { 
 

 
    var total = 
 
    $(e.delegateTarget) 
 
    .find('input') 
 
    .map(function() { 
 
     return parseFloat($(this).val()) || 0; 
 
    }) 
 
    .get() 
 
    .reduce(function(a, b) { 
 
     return a + b; 
 
    }); 
 

 
    $('#total').text(total); 
 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table class="table table-hover table-bordered" id="incomeId"> 
 
    <thead> 
 
    <tr> 
 
     <th> 
 
     sn 
 
     </th> 
 
     <th> 
 
     Title of income 
 
     </th> 
 
     <th> 
 
     Price 
 
     </th> 
 
     <th> 
 
     Action 
 
     </th> 
 
    </tr> 
 
    </thead> 
 
    <tbody> 
 
    <tr> 
 
     <td>1</td> 
 
     <td> 
 
     <select name="name" id="inputID" class="form-control"> 
 
      <option value=""> -- Select One --</option>       
 
     </select> 
 
     </td> 
 
     <td> 
 
     <input type="text" name="name" id="income_price" class="form-control income" value="" title="" required="required"> 
 
     </td> 
 
     <td> 
 
     <span class="glyphicon glyphicon-trash"></span> 
 
     </td> 
 

 
    </tr> 
 
    </tbody> 
 
    <tfoot> 
 
    <tr> 
 
     <td></td> 
 
     <td></td> 
 
     <td>Total: 
 
     <div class="total" id="total"></div> 
 
     </td> 
 
     <td></td> 
 
    </tr> 
 
    </tfoot> 
 
</table> 
 

 
<button type="button" class="btn btn-info" id="add-new">Add New Income</button>

+0

ありがとうございました。これはまさに私が求めていることです。他のものは、テーブルの新しい行を追加した後にテーブルのシリアル番号を増やす方法は?あなたはこれについて何か考えている場合は、共有してください。 –

+0

私は、DOMノードを解析するのではなく、要素のデータ属性を使用するか、内部的にカウンタを格納する方が好きです。しかし、あなたが知っていることを守るために、シリアル番号 'td'要素を見つけて、整数テキストを解析し、インクリメントして設定するのが最も簡単な方法です。あなたのために上記の例を更新しました。 (上記の例では入力を削除するなどの説明はないため、シリアル番号が改行される可能性があります) – Lee

+0

ありがとうございます。私に一つのことを教えてください。シリアルナンバーと行をうまく管理するにはどうすればいいですか? –

関連する問題