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

标签 javascript jquery html repeat calculated-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 字段上发生某些情况时计算总数。 它正确计算总计并将其分配给总金额字段。

/* 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

关于javascript - 如何在删除字段时计算输入字段值并将其分配给另一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42802943/

相关文章:

javascript - Twitter Bootstrap 适用于触摸屏吗?

javascript - 样式搜索 slider

javascript - 将控制添加到 Bootstrap 分页中每页的行数

html - 图像在 Chrome 中不会继承高度,但在 Firefox 中会

jquery - CSS 过渡不适用于图像

javascript - Google 可视化库加载会产生两个不同的结果

javascript - ScrollLeft 动画第二次尝试失败

jquery - 如何使用 jqmobile 将图像居中

php - 有关购物车 HTML 按钮中错误的帮助

javascript - 使用 CSS/Js 的文本动画无法正常工作