gpt4 book ai didi

javascript - 如何使用 jquery 清除和删除输入数据?

转载 作者:行者123 更新时间:2023-12-02 23:16:44 25 4
gpt4 key购买 nike

我正在尝试制作一个自动聚合数字并动态添加输入的应用程序。当我删除任何输入字段时,该数字不会从总和中减去。如何从我删除的条目中减去数字?感谢您的帮助。

var sayac = 1;
$(function() {
$('#ekle').click(function() {
sayac += 1;
$('#ekhizmetler tbody').append(
'<tr><th><strong></strong></th><td><input id="alan_" name="alanlar2[]' + '" type="text" class="form-control inst_amount" /></td><td><a href="#"class="delete btn btn-danger">Delete</a></td></tr>');
});

$('#ekhizmetler').on("click", ".delete", function(e) {
e.preventDefault();
$(this).closest("tr").remove();

})
});
function sumIt() {
var total = 0,
val;
$('.inst_amount').each(function() {
val = $(this).val();
val = isNaN(val) || $.trim(val) === "" ? 0 : parseFloat(val);
total += val;
});
$('#total_price').html(Math.round(total));
$('#total_amount').val(Math.round(total));
}

$(function() {
$("#add").on("click", function() {
$("#container input").last()
.before($("<input />").prop("class", "inst_amount").val(0))
.before("<br/>");
sumIt();
});

$(document).on('input', '.inst_amount', sumIt);
sumIt() // run when loading
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-12">
<table id="ekhizmetler" class="table table-condensed">
<thead>
<tr>
<th>Sıra</th>
<th>Numbers</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<th><strong>1</strong></th>
<td><input id="alan_2" name="alanlar2[]" type="text" class="form-control inst_amount"></td>
<td><input id="total_amount" name="total_amount" type="text" class="form-control" value="0"></td>
<td><a href="#" class="delete btn btn-danger">Delete</a></td>
</tr>
</tbody>
<tfoot>
<td>
<p id="ekle"><a href="#ekle" class="btn btn-primary">&raquo; New Field </a></p>
</td>
</tfoot>
</table>
</div>

最佳答案

删除输入行后只需调用 sumIt() ,这将仅根据 DOM 中当前现有的输入重新计算总和:

$('#ekhizmetler').on("click", ".delete", function(e) {
e.preventDefault();
$(this).closest("tr").remove();
sumIt(); // run after deletion
});

var sayac = 1;
$(function() {
$('#ekle').click(function() {
sayac += 1;
$('#ekhizmetler tbody').append(
'<tr><th><strong></strong></th><td><input id="alan_" name="alanlar2[]' + '" type="text" class="form-control inst_amount" /></td><td><a href="#"class="delete btn btn-danger">Delete</a></td></tr>');
});

$('#ekhizmetler').on("click", ".delete", function(e) {
e.preventDefault();
$(this).closest("tr").remove();
sumIt(); // run when loading
})
});
function sumIt() {
var total = 0,
val;
$('.inst_amount').each(function() {
val = $(this).val();
val = isNaN(val) || $.trim(val) === "" ? 0 : parseFloat(val);
total += val;
});
$('#total_price').html(Math.round(total));
$('#total_amount').val(Math.round(total));
}

$(function() {
$("#add").on("click", function() {
$("#container input").last()
.before($("<input />").prop("class", "inst_amount").val(0))
.before("<br/>");
sumIt();
});

$(document).on('input', '.inst_amount', sumIt);
sumIt() // run when loading
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-12">
<table id="ekhizmetler" class="table table-condensed">
<thead>
<tr>
<th>Sıra</th>
<th>Numbers</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<th><strong>1</strong></th>
<td><input id="alan_2" name="alanlar2[]" type="text" class="form-control inst_amount"></td>
<td><input id="total_amount" name="total_amount" type="text" class="form-control" value="0"></td>
<td><a href="#" class="delete btn btn-danger">Delete</a></td>
</tr>
</tbody>
<tfoot>
<td>
<p id="ekle"><a href="#ekle" class="btn btn-primary">&raquo; New Field </a></p>
</td>
</tfoot>
</table>
</div>

关于javascript - 如何使用 jquery 清除和删除输入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57134178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com