Ich muss Formularfelder dynamisch hinzufügen. Jede Gruppe von Eingabewerten enthält 3 Felder, quantity
, price
und total
. Jedes Mal, wenn ich numerische Werte in quantity
und price
einfüge, muss das Feld total
die Summe anzeigen oder multiplizieren.dynamische Formulare mehrere Eingabefelder berechnen
Ich habe dies realisiert, aber es funktioniert nur auf dem ersten Feld. Wenn ich eine weitere Reihe von Feldern hinzufüge, berechnet es andere nicht.
Hier ist mein Code.
$(document).ready(function() {
var i = 1;
$('#add').click(function() {
\t i++;
\t $('#articles').append('<tr id="row' + i + '"><td><input type="number" id="quantity" name="quantity[]" placeholder="quantity" class="form-control name_list" /></td> <td><input type="number" id="price" name="price[]" placeholder="price" class="form-control name_list" /></td> <td><input type="number" id="total" name="total[]" placeholder="total" class="form-control name_list" readonly /></td> <td><button type="button" name="remove" id="' + i + '" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function() {
\t var button_id = $(this).attr("id");
\t $('#row' + button_id + '').remove();
});
$('#submit').click(function() {
\t //alert($('#add_name').serialize()); //alerts all values and works fine
\t $.ajax({
\t \t url: "wwwdb.php",
\t \t method: "POST",
\t \t data: $('#add_name').serialize(),
\t \t success: function(data) {
\t \t \t $('#add_name')[0].reset();
\t \t }
\t });
});
function upd_art() {
\t var qty = $('#quantity').val();
\t var price = $('#price').val();
\t var total = (qty * price).toFixed(2);
\t $('#total').val(total);
}
setInterval(upd_art, 1000);
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<h2 align="center">title</h2>
<div class="form-group">
<form name="add_name" id="add_name">
<div class="table-responsive">
<table class="table table-bordered" id="articles">
<tr>
<td><input type="number" id="quantity" name="quantity[]" placeholder="quantity" class="form-control name_list" /></td>
<td><input type="number" id="price" name="price[]" placeholder="price" class="form-control name_list" /></td>
<td><input type="number" id="total" name="total[]" placeholder="total" class="form-control name_list" readonly /></td>
<td><button type="button" name="add" id="add" class="btn btn-success">Add new</button></td>
</tr>
</table>
<input type="button" name="submit" id="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>
</body>
</html>
für Vorspeisen, ids haben um eindeutig zu sein – baao
Dieses Skript funktioniert einwandfrei, wenn ich Werte in die Datenbank einfüge. Das Ziel besteht darin, die Wertsumme nicht manuell einzufügen, sondern automatisch zu berechnen und dann in db zu speichern. Wenn möglich, ein Beispiel, wie Sie in diesem Fall mit eindeutigen IDs rechnen können. Danke – sigma