如何在删除字段时计算输入字段值并将其分配给另一个字段

How to calculate input fields values and assign it to another field while removing a field

我正在尝试计算一组重复字段的总量。

这是我的标记

<label for="name" class="p-label-required">Amount <span style="color: red"> (Numbers only)</span></label>
<input type="text" id="amount" name="amount[]" placeholder="Amount" required="required" class="form-control inputChangeVal reqF" data-js-input-type="number" /><br/>

<div class="add_another_st"></div>
<button class="btn add_another_st_btn" style="margin-bottom: 25px;">+</button>
<hr style="margin-top: 0;">
Total Amount:
<input type="text" class="totalAmount" id="updatedTotalAmount" style="font-size: 20px; font-weight: bold; margin-bottom: 25px; border: 0;" readonly />

这是我的 jQuery,每当 amount 字段上发生某些事情时计算总数。. 它会正确计算总数并将其分配给 total amount 字段。

/* calculating and updating the total amount */
    function updateTotal() {
        var price = 0;

        jQuery(".inputChangeVal").each(function() {
            var t = parseFloat(jQuery(this).val(), 10);
            price = price + t;
        });

        var total = price.toFixed(2);
        jQuery(".totalAmount").val(total);
        console.log('updateTotal Runs');
    }
    jQuery(document).on("change, keyup", ".inputChangeVal", updateTotal);

但我想在删除金额字段时再次计算金额,但它不起作用/每当删除金额字段时更新总计。

我为此创建的代码在 js fiddle

如何在删除字段后再次更新总数?

wrapper元素向上滑动需要调用updateTotal()函数

jQuery(this).parent('div').slideUp(1000, function() {
    jQuery(this).remove();
    x--;
    updateTotal(); // updating total
});

Working fiddle

jQuery(wrapper).on("click", ".remove_field", function(e) { //user click on remove text
        debugger;
        e.preventDefault();
        jQuery(this).parent('div').slideUp(1000, function() {
                jQuery(this).remove();

                x--;
        /*running again calculation 0f the total amount and while removing an amount */
        var price = 0;
        jQuery(".inputChangeVal").each(function() {
                var t = parseFloat(jQuery(this).val(), 10);
                price = price + t;
        });
        var total = price.toFixed(2);
        jQuery(".totalAmount").val(total);
        console.log('updateTotal Runs while removing');
         });
});