jQuery计算插件&按选择更改成本
我有一个表格可以计算订单的总成本,但是如果一件商品的价格越大,我就无法解释这一点。有人有什么想法吗?见下面的示例。字段和计算插件可从jQuery计算插件&按选择更改成本,jquery,plugins,Jquery,Plugins,我有一个表格可以计算订单的总成本,但是如果一件商品的价格越大,我就无法解释这一点。有人有什么想法吗?见下面的示例。字段和计算插件可从 添加大小作为参数,并从描述中解析出额外成本。下面的代码未经测试,但应该可以工作 <script type="text/javascript"> $(document).ready(function (){ function recalc(){ // run the calc() method on e
添加大小作为参数,并从描述中解析出额外成本。下面的代码未经测试,但应该可以工作
<script type="text/javascript">
$(document).ready(function (){
function recalc(){
// run the calc() method on each of the "total" fields
$("[id^=total_item]").calc(
// the equation to use for the calculation
"qty * (price + extraCharge)",
// we now define the values for the variables defined in the equation above
{
// instead of using a static value, we use a jQuery object which grabs all the quantities
qty: $("input[name^=qty_item_]"),
// now we define the jQuery object which reads in the "price" from the table cell
price: $("[id^=price_item_]"),
extraCharge: function() {
selectedSize = $('#sSize').val();
size = $('option[value=' + selectedSize + ']').text();
charge = 0;
if (size.indexOf('$') != -1) {
charge = parseFloat(size.substring(size.indexOf('$'), size.indexOf(')')));
}
return charge;
}
},
// this function is execute after the calculation is completed, which allows us to
// add formatting to our value
function (s){
// return the number as a dollar amount
return "$" + s.toFixed(2);
},
// once all calculations are completed, we execute the code below
function ($this){
// now we get the sum() of all the values we just calculated
var sum = $this.sum();
// now that we have the grand total, we must update the screen
$("#grandTotal").text(
// round the results to 2 digits
"$" + sum.toFixed(2)
);
}
);
}
// bind the recalc function to the quantity fields
$("input[name^=qty_item_]").bind("keyup", recalc);
});
</script>
P>个人认为它是一个插件,我建议用户考虑将每个产品的大小做成不同的产品,这意味着你可以保持产品的简单性和通用性。 如果你认为需要一个更高级的系统,那么提供一个可选的参数,允许他们传递一个函数,该函数使用他们需要的任何参数计算并返回该项目的成本,这将允许插件的用户拥有一个更精细的成本计算系统 我认为这是你最好的选择,因为人们用户有发明成本计算规则的习惯,以世界上最好的意愿,你不可能想到所有这些规则
现在我已经回答了你的问题,我想说的是,成本或其他重要数据的任何验证都应该毫无例外地在服务器上完成。事实上,我已经测试了这一点,并使其正常工作。它还允许表单上有多个大小字段
$("[id^=sSize_]").change(function(){
var strLength = (this.id.length);
var numbr = this.id.substring(strLength, strLength-1);
//alert(numbr);
$("[id=price_item_"+numbr+"]").text('$42.99');
});
$("[id^=sSize_]").change(function(){
var strLength = (this.id.length);
var numbr = this.id.substring(strLength, strLength-1);
//alert(numbr);
$("[id=price_item_"+numbr+"]").text('$42.99');
});