Skip to main content
Participant
March 31, 2024
Frage

Subtracting 3 form fields without continuing to minus their result when inputting other form field

  • March 31, 2024
  • 1 Antwort
  • 711 Ansichten

Hello, please help me.

Is there a custom calculation script for this to happen? I have 3 fields to be subtracted from the first form field and when I input other form filled they keep subtracting from each other (only the 3 form filled)

this is my calculation script:

 

var a = this.getField("3").value;

var b = this.getField("2").value;

var c = this.getField("1").value;

event.value = a;

if (a !=="" && b !=="") {event.value = a - b;}

if (a !=="" && c !=="") {event.value = a - c;} ;

;

 

 

So the final result of their difference would be on Field 3.

Please tell me where I went wrong on this one. I also tried setting their calculation order it just keeps messing up. Or is there a custom script for this to prevent from subtracting furthermore.

 

Thank you! I use adobe acrobat

Dieses Thema wurde für Antworten geschlossen.

1 Antwort

Thom Parker
Community Expert
Community Expert
March 31, 2024

Calculation scripts are run every time any field on the form changes. Using the value of the field in a calculation script on that field creates a feedback loop, not good.  To fix this issue you need to add another form field to hold the field "3" value and break the loop. 

 

BTW: using numbers as field names is a bad practice. Use short descriptive names. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
March 31, 2024

Thank you for your response.

 

So what you mean is , there is no way that I can calculate 2 fields to result on the first? That's why it creates a loop? 

Participant
March 31, 2024

A feedback loop is created when a calculated result is fed back into the calculation chain that creates the result.   

This does not work well in any context and can lead to some bad results. 

 

So, maybe there is a better approach. What exactly are you trying to do?

 

 


Attached is an example of how I want to do. 

First, I want to type in Box1 whatever I want to type then when I input a number in Box2 will be subtracted from Box1. After that, Inputting a number in Box3 will be subtracted from the difference of Box1-Box2. Then after that, when I input all the other data from other form fields they still keep subtracting from each other. Is there any way to prevent that?