0
セル内の入力値を変更し、jquery onkeyupイベントを使用してテーブル内で数学演算を実行したいとします。これを行うにはテーブル内の計算を実行するDataTablesとjQuery
<table id="GVHabvar">
<tbody>
<tr>
<td>
<input id="cantidad" name="cantidad" type="text" class = "calculo">
</td>
<td>
<input id="precio" name="precio" type="text" class = "calculo">
</td>
<td>
<input id="valor" name="valor" type="text">
</td>
</tr>
<tr>
<td>
<input id="cantidad" name="cantidad" type="text" class = "calculo">
</td>
<td>
<input id="precio" name="precio" type="text" class = "calculo">
</td>
<td>
<input id="valor" name="valor" type="text">
</td>
</tr>
<tr>
<td>
<input id="cantidad" name="cantidad" type="text" class = "calculo">
</td>
<td>
<input id="precio" name="precio" type="text" class = "calculo">
</td>
<td>
<input id="valor" name="valor" type="text">
</td>
</tr>
</tbody>
</table>
これは私の試みですが、それは(例えばページネーションを持つ)のDataTableとうまく動作しない
$(document).ready(function() {
var filas = $("#GVHabvar tbody tr");
filas.each(function (index) {
var fila = $(this);
fila.find('.calculo').on('keyup', function() {
var cantidad = ($.isNumeric(fila.find("#cantidad").val())) ? fila.find("#cantidad").val() : 0;
var precio = ($.isNumeric(fila.find("#precio").val())) ? fila.find("#precio").val() : 0;
var valor = parseInt(cantidad, 10) * parseInt(precio, 10);
fila.find('#valor').val(valor);
});
});
});
作品です! – DavidM