Copy link to clipboard
Copied
Complete Javascript novice here. I'm creating a form which is fairly complex and I can't get a ticked checkbox to even fill a text feild let alone do what I actually need it to do. Hopefully someone can help with the code I need.
The form involves clients listing products and $ value investments they would like for each product. Those investments fill in a 'Subtotal' field easily with a sum. Depending on the amount of products they are getting, a discount can be applied. 2 products = 10% off, 3 products = 15% off, 4+ products = 20% off. Below the Subtotal field I want a Subtotal with discount applied (SubtotalDiscount) field. I've currently got checkboxes for the three discount options. When one of the checkboxes are ticked, I want a calculation script to take the value in the 'subtotal' field, calculate the corresponding %, then add that value to the 'subtotal' value and place it in the 'SubtotalDiscount' field.
Does anyone have any clue on the code I would need for this?
Copy link to clipboard
Copied
I got it working with some different code. Hooray! For anyone's reference, this is what I added to the 'SubtotalDiscount' field custom calculation script...
var percentage = 0;
if (this.getField("2Products").value == "On") { percentage = 0.10 }
if (this.getField("3Products").value == "On") { percentage = 0.15 }
if (this.getField("4Products").value == "On") { percentage = 0.20 }
var subtotal = this.getField('Subtotal').value;
var discount = subtotal * percentage;
this.event.value = subtotal - discount;
Tomorrow I might change them from checkboxes to radial buttons so that they can't tick two at a time, but I think that won't be too hard. Have also rearranged them so that if two checkboxes are ticked that it prioritises the largest discount.
Copy link to clipboard
Copied
Here an article that will get you going in the right direction. It includes a sample file that uses a checkbox.
https://www.acrobatusers.com/tutorials/conditional-execution/
Copy link to clipboard
Copied
Thank you. I have already read through all of that and tried to use the sample file code, but it's not doing the same things as I need it to do and without knowing how to write and read javascript I'm a bit lost!
Copy link to clipboard
Copied
Use a calculation script on the SubtotalDiscount field
var cDiscountSelection = this.getField("Checkbox").valueAsString;
if(cDiscountSelection == "choice1")
event.value = this.getField("Subtotal").value * .9;
else if(cDiscountSelection == "choice2")
event.value = this.getField("Subtotal").value * .8;
... etc ..
I don't know the name of the checkbox group or the export values, so you'll need to replace those in the code. The "Subtotal", as well as all the other field names have to be verbatim. Capital letters and all.
Copy link to clipboard
Copied
Thanks Thom!
I've tried that out. I have my 3 checkboxes grouped and have their respective export values now. I've got my code on the SubtotalDiscount text field. Saved. But checking any of the boxes isn't putting any value into the field. Can you spot what I'm doing wrong?
Copy link to clipboard
Copied
There's nothing wrong with the code. But perhaps the values and/or field names aren't lining up quite right.
We'll need to do some debug.
Look in the Console window to see if any errors are reported.
There is a tutorial here, on using the console and doing some debug.
https://www.pdfscripting.com/public/Free_Videos.cfm#JSIntro
Copy link to clipboard
Copied
I got it working with some different code. Hooray! For anyone's reference, this is what I added to the 'SubtotalDiscount' field custom calculation script...
var percentage = 0;
if (this.getField("2Products").value == "On") { percentage = 0.10 }
if (this.getField("3Products").value == "On") { percentage = 0.15 }
if (this.getField("4Products").value == "On") { percentage = 0.20 }
var subtotal = this.getField('Subtotal').value;
var discount = subtotal * percentage;
this.event.value = subtotal - discount;
Tomorrow I might change them from checkboxes to radial buttons so that they can't tick two at a time, but I think that won't be too hard. Have also rearranged them so that if two checkboxes are ticked that it prioritises the largest discount.
Copy link to clipboard
Copied
That last line of code should be:
event.value = subtotal - discount;
Copy link to clipboard
Copied
Name all of your radio buttons the same and give them different export values, and they'll act just like radio buttons. But whether or not you use radio buttons or checkboxes, you'll need to use the code I provided in the first post.
Also, it is best to write the "if" block as an "else if" so that there is only one path to each answer.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more