在Jquery中,根据输入值的百分比计算费用的最佳方法是什么

时间:2014-01-10 16:33:36

标签: javascript jquery

我有一个脚本制作,它将成为债务顾问的工作表。在工作表上是输入字段,用于收集不同类型的债务,如信用卡和抵押贷款,这些债务总计为一个名为subTotal的变量。我还有一个输入字段,它将被设置为变量percent。此输入字段是介于5和30之间的可调整数字,表示百分比。

我正在试图找出获取这些变量并进行费用计算的最佳方法。结果应该是小计的百分比。例如,如果var subTotal总计200并且有人将15输入到由变量percent表示的输入字段中。计算结果应为30

以下是变量

var subTotal = self.calculateTotalFor(elems);
// this the summed total for the debt input fields

var percent = $("#percent_field")
//I'm not sure if this is the best way to set this value because it needs to be a percent

然后我需要获取这些变量并获得费用计算,但我知道这种方法不起作用,因为它不会在decimal值前面添加percent。< / p>

total += (percent - 1) * subTotal;

任何想法都会有所帮助,因为我只是一个新手,谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

var subTotal = self.calculateTotalFor(elems);
var percent = $("#percent_field").val(); // <------ The value....
var total = percent * subTotal / 100;