简体   繁体   中英

Changing one field causes other prefixed fields to change - Adobe Acrobat Standard DC

There are two fields in my form that changes when I insert text/number in other fields, why? They are not connected, for instance if I write my full name in a requested field (refering to the taken screenshot), it changes two fields "Fratrukket Rabatt (Discount price)" and "Total pris (total price)".

Fratrukket Rabatt (Discount price): Calculates the total discount

// Get first value as number 
var v1 = +getField("Rabatt i prosent").value;

// Get second value as number 
var v2 = +getField("Total pris").value;

// Calculate the result
event.value = (v1 / 100) * v2; 

Total pris (total price): Calculates the total price substracting discount

// Get first value as number 
var viva1 = +getField("Pris per dekk").value;

// Get second value as number 
var viva2 = +getField("Antall dekk").value;

// Get third value as number 
var viva3 = +getField("Pris på arbeid").value;

// Get fourth value as number 
var viva4 = +getField("Fratrukket Rabatt").value;

// Calculate the result
event.value = ((viva1 * viva2) + viva3) - viva4;  

Is this a common problem, please help.

在此处输入图片说明

This is absolutely correct behavior, you encounter.

Having a look at the Acrobat JavaScript documentation (part of the Acrobat SDK documentation, downloadable from the Adobe website), there is an explication and a diagram of the field event sequence. Relevant for our issue is that the Calculate event is part of that sequence, and whenever a field value is changed, the complete Calculation sequence gets executed. So, if you have some default values, and some calculations using them, the calculated field values get recalculated.

Now, there is, however, quite a bit of a confusion in the logic itself, and there is little chance to get reliable results to begin with.

First and foremost, it is considered Best Practice to consolidate all calculations (of a calculation chain) into one single script, and either attach it to the last result field of the calculation chain, or an invisible, read-only field which is otherwise not involved in anything (the field event sequence is the reason why this works).

Based on what can be concluded from the example, your script in the Total Pris field could look like this:

Note that, in fact, there is a logical flaw in the calculation, because the discount relies on the grand total which is calculated after the discount gets calculated.

var deckprice = this.getField("Pris per dekk").value * this.getField("Antall dekk").value ;
var subtotal = deckprice + this.getField("Pris på arbeid").value*1 ;
var discount = subtotal * this.getField("Rabatt i prosent").value / 100 ;
var grandtotal = subtotal - discount ;

this.getField("Fratrukket Rabatt").value = discount ;
event.value = grandtotal ;

And that should do it.

If you are using this calculation in an independent field (not in the "Total Pris" field), you would change the last line to

this.getField("Total Pris").value = grandtotal ;

So much for the calculation.

Another suggestion (well, it is a bit of a pet peeve of mine…): I know that automatic field recognition in Acrobat is very convenient. However, the resulting field names are not very useful, and (more important) it prevents the user from doing a serious analysis of the form, which then leads to logical errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM