jquery - How to perform multiplication in javascript for dynamically added field on html? -
jquery - How to perform multiplication in javascript for dynamically added field on html? -
here html source code 1 pair of dynamically added elements unit_price
, qty
.
<label class="decimal required control-label" for="order_order_items_attributes_1413563163040_unit_price"> <input id="order_order_items_attributes_1413563163040_unit_price" class="numeric decimal required span5 span5" type="number" step="any" name="order[order_items_attributes][1413563163040][unit_price]"> </div> <div class="input integer required order_order_items_qty"> <label class="integer required control-label" for="order_order_items_attributes_1413563163040_qty"> <input id="order_order_items_attributes_1413563163040_qty" class="numeric integer required span5 span5" type="number" step="1" name="order[order_items_attributes][1413563163040][qty]"> </div>
whenever there alter in unit_price, can do:
// find ids match pattern: order_order_items_attributes_xxxxxxxxxxxxx_unit_price var orderitemregex = /^order_order_items_\d+_unit_price$/; $("[id^='order_order_items_']").filter(function(index) { homecoming orderitemregex.test(this.id); }).change(function() { //here needs done. there may more 1 pair of unit cost , qty on form. each pair of unit cost & qty { total += unit_price * qty } //update total $('order_total').val(total); });
the same process repeated 1 time again qty whenever there qty alter in qty field.
we looking solution iterates through list , retrieves each of id of unit cost , qty. many thanks.
suggestions:
build array containing numeric portions of ids price/qty fields. regex approach close, needed tweaked extract portion of id.
build function loop through numbers, utilize them fetch price/qty pairs form, , calculate subtotal.
call function page loads (only necessary if form prepopulated).
call function whenever cost or quantity changes.
working example:
note: added values form fields convenience. alter them see subtotal update. , you'll have add together more code or field validation rules business relationship negative numbers, etc.
class="snippet-code-js lang-js prettyprint-override"> $(document).ready(function () { // find ids match pattern: order_order_items_attributes_xxxxxxxxxxxxx_unit_price var orderitemregex = /^order_order_items_attributes_(\d+)_unit_price$/; var $editablefields = $("input[id$='_unit_price'], input[id$='_qty']"); var fieldnumbers = (function() { var results = []; $("[id$='_unit_price']").each(function () { var numericid = orderitemregex.exec(this.id)[1]; results.push(numericid); }); console.log(results); return results; })(); function updatesubtotal() { var subtotal = 0; $.each(fieldnumbers, function (index, fieldnumber) { var pricestring = $('#order_order_items_attributes_' + fieldnumber + '_unit_price').val(); var priceamount = pricestring ? parsefloat(pricestring.replace(/\$\,/g, '')) : 0; var quantitystring = $('#order_order_items_attributes_' + fieldnumber + '_qty').val(); var quantityamount = quantitystring ? parsefloat(quantitystring.replace(/\,/g, '')) : 0; subtotal += (priceamount * quantityamount); }); $('#subtotal').text('$' + subtotal.tofixed(2)); } updatesubtotal(); $editablefields.change(updatesubtotal); });
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <label class="decimal required control-label" for="order_order_items_attributes_1413563163040_unit_price">price</label> <input id="order_order_items_attributes_1413563163040_unit_price" class="numeric decimal required span5 span5" type="number" step="any" name="order[order_items_attributes][1413563163040][unit_price]" value="5.95"/> </div> <div class="input integer required order_order_items_qty"> <label class="integer required control-label" for="order_order_items_attributes_1413563163040_qty">quantity</label> <input id="order_order_items_attributes_1413563163040_qty" class="numeric integer required span5 span5" type="number" step="1" name="order[order_items_attributes][1413563163040][qty]" value="1"/> </div> <label class="decimal required control-label" for="order_order_items_attributes_1413563163052_unit_price">price</label> <input id="order_order_items_attributes_1413563163052_unit_price" class="numeric decimal required span5 span5" type="number" step="any" name="order[order_items_attributes][1413563163052][unit_price]" value="0.78"/> </div> <div class="input integer required order_order_items_qty"> <label class="integer required control-label" for="order_order_items_attributes_1413563163052_qty">quantity</label> <input id="order_order_items_attributes_1413563163052_qty" class="numeric integer required span5 span5" type="number" step="1" name="order[order_items_attributes][1413563163052][qty]" value="3"/> </div> <div> <label>subtotal:</label> <span id="subtotal"></span> </div>
javascript jquery html
Comments
Post a Comment