问题描述
计算字段Acrobat pdf Javascript,当我跳过一个字段时,总金额显示不正确,但是当每个字段具有从上到下的值时,它可以正常工作。怎么了?
var fields = this.getField("amount");
var a = fields.getArray();
var sum = 0;
for (i = 0; i < a.length; i++){
sum += a[i].value;
//I tried to place zeros in the fields if there are no values
if (fields.value == null){
fields.value = 0;
}
}
//This code accept the total amount and also hides the default zeros
if (sum > 0){
event.value = sum;
}
else {
event.value = "";
}
解决方法
在这样的循环中,我通常会在比较总计之前,通过将字段____|____Name______|_birthdate_|____company_name_______
0 |Steve Smith |1995-01-26 |Sharp,Reed and Crane
1 |Joe Nadal |1955-08-14 |Price and Sons
2 |Roger Federer |2000-06-28 |Pruitt,Bush and Mcguir
与字段value
进行比较来检查字段是否具有值。该行如下所示:
defaultValue
通常,在值级别进行比较就足够了,但是如果0是合法值,则比较必须包括类型。然后看起来像这样:
if (this.getField("myField").value != this.getField("myField").defaultValue) {
那应该做到的。
,为解决此问题,Javascript加法运算符``+`''也可用于添加(连接)字符串。空字段充当字符串,因此您需要通过在值后面加上+来转义字符串。
sum += +a[i].value;
它与var fields = +this.getField("amount");