这似乎是一个简单的问题。
我的xPage上有几个编辑框。用户可能输入也可能不输入整数值。字段设置为接受整数。
我想要计算这些字段中的所有值。我有以下,这是有效的,但它似乎非常低效。有更简单的方法吗?
var DeliveryTotal:int = getComponent(ctlName).getValue() == null ? 0 : parseInt(getComponent(ctlName).getValue());
var Delivery100s:int = parseInt(getComponent("inputTextDelivery100s").getValue());
var Delivery50s:int = parseInt(getComponent("inputTextDelivery50s").getValue());
var Delivery20s:int = parseInt(getComponent("inputTextDelivery20s").getValue());
var Delivery10s:int = parseInt(getComponent("inputTextDelivery10s").getValue());
var Delivery5s:int = parseInt(getComponent("inputTextDelivery5s").getValue());
var Delivery1s:int = parseInt(getComponent("inputTextDelivery1s").getValue());
DeliveryTotal = isNaN(DeliveryTotal) ? 0 : DeliveryTotal;
Delivery100s = isNaN(Delivery100s) ? 0 : Delivery100s;
Delivery50s = isNaN(Delivery50s) ? 0 : Delivery50s;
Delivery20s = isNaN(Delivery20s) ? 0 : Delivery20s;
Delivery10s = isNaN(Delivery10s) ? 0 : Delivery10s;
Delivery5s = isNan(Delivery5s) ? 0 : Delivery5s;
Delivery1s = isNan(Delivery1s) ? 0 : Delivery1s;
var totalEntered = Number(Delivery100s) + Number(Delivery50s) + Number(Delivery20s);
答案 0 :(得分:1)
您可以稍微简化脚本。制作组件名称数组。然后使用getValue()和?将代码缩减为两行。条件并在循环内为那些组件名称数组迭代它。它可能会更清洁。
答案 1 :(得分:1)
你能计算客户端的价值吗?如果是这样,那么你可以使用以下jQuery。它选择id为以inputTextDelivery开头的所有字段,并将值一起添加。
Number(0 + ..)将空白转换为0,0123将转换为123
var sum=0;
$('[id^=inputTextDelivery]').each(function() {
sum += Number(0+$(this).val());
});
如果没有,你当然可以通过
删除检查0的代码块var totalEntered = Number(0+Delivery100s) + Number(0+Delivery50s) + Number(0+Delivery20s);