0
我试图代码使用优秀calculation plugin的订单价格计算的解决方案。我的表单有两种不同的价格,一种是10以下的数量,另一种是数量超过这个数量的价格。
这是我到目前为止的代码:
$(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)
);
}
);
}
});
已经很接近了,但它被设置为外地阵列中的第一项的值useprice停留。我想我需要将用量计算'在循环内部'以某种方式,但我坚持如何。
更新:Demo page here。
按照惯例,任何&所有帮助感激地收到。 TIA :)
嗨托马斯,感谢您的意见,但这不是很不幸。我更新了代码,将数量强制为一个整数并放在一起演示页面。 – da5id 2010-10-17 01:29:49
@ da5id:我编辑了我的答案,为您提供了一个工作代码片段。它应该按照您现在的要求工作。 – Thomas 2010-10-17 08:40:27
干杯托马斯,就像一个魅力。我今天早上醒来时以为我必须从完全不同的角度来看待它,所以我不能告诉你你有多少时间救了我。真的很感谢你去的长度。再次感谢:) – da5id 2010-10-17 20:48:41