[英]How to calculate fields in Acrobat PDF using Javascript?
Calculating fields Acrobat pdf Javascript the total amount displays incorrectly when I skip a field but It works correctly when each field has a value from top to down.计算字段 Acrobat pdf Javascript 当我跳过一个字段时,总金额显示不正确,但是当每个字段从上到下都有一个值时它可以正常工作。 What is wrong?
怎么了?
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 = "";
}
In such a loop, before adding up the running total, I usually check whether a field has a value by comparing its value
with its defaultValue
.在这样的循环中,在将运行总数相加之前,我通常通过将其
value
与其defaultValue
进行比较来检查字段是否具有值。 The line looks like this:该行如下所示:
if (this.getField("myField").value != this.getField("myField").defaultValue) {
Normally, comparing on value level is sufficient, but if 0 is a legal value, the comparison has to include the type.通常,在值级别上进行比较就足够了,但如果 0 是合法值,则比较必须包括类型。 Then it looks like this:
然后它看起来像这样:
if (this.getField("myField").value !== this.getField("myField").defaultValue) {
And that should do it.那应该这样做。
To fixed this, Javascript Addition operator ``+``` can also be used to add (concatenate) strings.为了解决这个问题,Javascript 加法运算符 ``+``` 也可用于添加(连接)字符串。 The empty field was acting as string so you will need to escape the string by appending + to the values.
空字段充当字符串,因此您需要通过将 + 附加到值来转义字符串。
sum += +a[i].value;
It is the same as var fields = +this.getField("amount");
同
var fields = +this.getField("amount");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.