Multiplying a Text Field if a Checkbox is Checked, while freely being able to Uncheck it.
Copy link to clipboard
Copied
I want to have a text field, "MA", with an initial value typed into it ("10"). When a seperate checkbox is checked ("CRITICAL1"), I want the value in MA to be halved ("5"). The script I wrote does that, but I'd also like those changes to revert when the box is unchecked. As it works now, unchecking the box does not change MA back to 10 from 5, and a subsequent recheck would halve MA again to sqrt(5).
The script below is a fix I tried to implement, where I multiply by 2 when the box is unchecked to cancel out the halving, but I've ran into a different problem where the initial number I input into MA is doubled from the start (e.g. when filling out MA for the first time, I write "10" and it becomes "20"). It's important to the document that MA works with any input, not just the one's I've used in my example. Any help?
var v1 = this.getField("CRITICAL1").isBoxChecked(0);
function test(){
if(v1){event.value = event.target.value*0.5;}
else{event.value = event.target.value*2}
}
Copy link to clipboard
Copied
Why did you put the code in a function, and then not call it?
Copy link to clipboard
Copied
The real problem is that your code will run over and over again, each time the value of any field is changed, and will therefore continue to make the value smaller (or larger). You need to either make the code dependent on the triggering field being the check-box in question, or move it to the check-box itself and have it change the value of the text field, instead of putting it under the text field's own calculation script.
Copy link to clipboard
Copied
To add to Try67's analysis. If a field value needs to be changed in a certain way, then the original value has to be preserved. Otherwise it can never go back. Calculations need to be a linear progression. It starts somewhere stable and then goes somewhere else. You don't have a stable starting place. Think about that.
Using the MouseUp event will provide an approximation. But it is not restoring the original value. Each time the number is divided and multiplied it losses data. Depending on the initial value, it could either maintain a stable value, or become unstable. So this is not a good strategy.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Hi,
You could use a global variable and an On Blur Action Script...
// Document Script
if (this.getField("CRITICAL1").isBoxChecked(0)) global.realValue=this.getField("MA").value*2;
else global.realValue=this.getField("MA").value;
// On Blur "MA" Field Script
global.realValue=event.value;
if (this.getField("CRITICAL1").isBoxChecked(0)) this.getField("MA").value=global.realValue*0.5;
// Mouse Up "CRITICAL1" Check Box Field Script
if (event.target.value!="Off") this.getField("MA").value=global.realValue*0.5;
else this.getField("MA").value=global.realValue;
@+
Copy link to clipboard
Copied
[MOVED TO THE ACROBAT DISCUSSIONS]
Acrobate du PDF, InDesigner et Photoshopographe

