Greetings,
I am trying to code a solution for an order form pricing calculator using the excellent calculation plugin. My form has two different prices for each item, one for quantities under 10 and one for quantities over that amount.
This is the code I have so far:
$(document).ready(function() {
// bind the recalc function to the quantity fields
$("input[name^=qty_item_]").bind("keyup", recalc);
// run the calculation function now
recalc();
function recalc() {
var quantity = $("input[name^=qty_item_]").val();
var quantity_int = parseInt(quantity_str);
var threshold = 10;
if (quantity_int < threshold) {
var useprice = $("[id^=price_item_1_]");
} else {
var useprice = $("[id^=price_item_2_]");
}
$("[id^=total_item_]").calc(
// the equation to use for the calculation
"qty * price",
// define the variables used in the equation, these can be a jQuery object
{
qty: $("input[name^=qty_item_]"),
price: useprice
},
// define the formatting callback, the results of the calculation are passed to this function
function (s){
// return the number as a dollar amount
return "$" + s.toFixed(2);
},
// define the finish callback, this runs after the calculation has been complete
function ($this){
// sum the total of the $("[id^=total_item]") selector
var sum = $this.sum();
$("#grand_total").text(
// round the results to 2 digits
"$" + sum.toFixed(2)
);
}
);
}
});
It's close, but useprice stays at the value it is set for for the first item in the field array. I'm thinking I need to bring the useprice calculation 'inside the loop' somehow, but I'm stuck as to how.
UPDATE: Demo page here.
As per usual, any & all help gratefully received. TIA :)