Javascript jquery通过html元素数组循环并计算第三个元素输入中的值

Javascript jquery通过html元素数组循环并计算第三个元素输入中的值,javascript,jquery,Javascript,Jquery,我有下面的输入表单,用户可以在按下按钮时动态添加更多的输入字段。我想用jquery做的是,每当我在相应的替换零件数量[]或替换零件价格[]上有.keyup事件时,更新所有的total_cost[]字段 <div class="form-group col-xs-1"> <label>Part price</label> <input type="number" class="form-control" name="replaced_par

我有下面的输入表单,用户可以在按下按钮时动态添加更多的输入字段。我想用jquery做的是,每当我在相应的替换零件数量[]或替换零件价格[]上有.keyup事件时,更新所有的total_cost[]字段

<div class="form-group col-xs-1">
    <label>Part price</label>
    <input type="number" class="form-control" name="replaced_part_price[]" id="replaced_part_price">
</div>
<div class="form-group col-xs-1">
    <label>Part Qty</label>
    <input type="number" class="form-control" name="replaced_part_qty[]" id="replaced_part_qty">
    </div>
<div class="form-group col-xs-1">
    <label>Total cost</label>
    <input type="number" class="form-control" name="total_cost[]" id="total_cost" readonly>
</div>

我在这篇文章中找到了答案:并将其调整为与DINAMICALY添加的字段一起工作

代码如下:

<div id="container1">
<button class="btn btn-primary btn-md add_more_button" id="add_form_field">New Line &nbsp; <span>+ </span></button>
    <div class="form-group col-xs-3">
        <label>Price</label>
            <input type="number" class="form-control a" name="pret_piesa_inlocuita[]">
        <label>Qty</label>
            <input type="number" class="form-control b" name="cantitate_piesa_inlocuita[]">
        <label>Piece Price</label>
            <input type="number" class="form-control price" name="cost_piese[]" readonly>
    </div>
</div>

所以当你说添加新行时,你的意思是添加新行?其中包括总零件和数量输入,您应该创建一个最小的示例来正确描述您的问题,因为如果要添加新行,则必须包括添加新行script@MuhammadOmer Aslam,我最终用这里的答案找到了一个修复方法:。谢谢你的帮助
<div id="container1">
<button class="btn btn-primary btn-md add_more_button" id="add_form_field">New Line &nbsp; <span>+ </span></button>
    <div class="form-group col-xs-3">
        <label>Price</label>
            <input type="number" class="form-control a" name="pret_piesa_inlocuita[]">
        <label>Qty</label>
            <input type="number" class="form-control b" name="cantitate_piesa_inlocuita[]">
        <label>Piece Price</label>
            <input type="number" class="form-control price" name="cost_piese[]" readonly>
    </div>
</div>
$(add_button).click(function(e){ 

var wrapper = $("#container1"); 

        $(wrapper).append('<div class="form-group col-xs-3">\
        <label>Price</label>\
            <input type="number" class="form-control a" name="pret_piesa_inlocuita[]">\
        <label>Qty</label>\
            <input type="number" class="form-control b" name="cantitate_piesa_inlocuita[]">\
        <label>Piece Price</label>\
            <input type="number" class="form-control price" name="cost_piese[]" readonly>\
        </div>');
    }
});
$(document).on('keyup', '.a,.b', function() {
    var textValue1 = $(this).parent().find('.a').val();
    var textValue2 = $(this).parent().find('.b').val();
$(this).parent().find('.price').val(textValue1 * textValue2); 
    var sum = 0;
$('.price').each(function() {
   sum += +$(this).val();
   });
});