Adding dynamic check and radio box values to total sum

Explorer ,
Apr 04, 2021 Apr 04, 2021

Copy link to clipboard

Copied

Okay, so I am trying to build a risk assessment calculator. This requires me to add up the boxes riskFactor1a, riskFactor2a, riskFactor3a, riskFactor4a, and riskFactor5a (the a is important because this gets repeated 4 times with different types of risk and those are b, c, and d). However, the slightly more complicated bit is that I need to add these up but not let them exceed 10. If their sum exceeds ten, the textbox should still only display 10. This seems remarkeably simple yet it is proving weirdly frustrating and I cannot get it to work right. If anyone has a good answer for this I would greatly appreciate it, thank you in advance. 

TOPICS
Edit and convert PDFs, How to, JavaScript, PDF forms

Views

187

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct Answers

Adobe Community Professional , Apr 04, 2021 Apr 04, 2021
Use this: if(total == 0) event.value = ""; else if (total > 10) event.value = 10; else event.value = total;  

Likes

translate

Translate

Translate
Adobe Community Professional , Apr 04, 2021 Apr 04, 2021
Use what Bernd suggested, just fix mistype ( If to if) and also change:total+riskFactorto:total+=riskFactor

Likes

translate

Translate

Translate
Adobe Community Professional ,
Apr 04, 2021 Apr 04, 2021

Copy link to clipboard

Copied

 

 

If you want to add all the numeric export values assigned to each of  these radio buttons with, change the names for each  fields  using a parent-child naming hierarchy and employe the getArray() method.

 

For example, instead of naming all of these fields like:

 

riskFactor1a

riskFactor1b

riskFactor1c

riskFactor1d

 

Use a prefix like :

 

risk.Factor1a

risk.Factor1b

risk.Factor1c

 

But, even easier, you can just create all of them under the same group name "risk", for example,  and then assign a different export value to each radio button.

 

This will make them mutually exclusive, and also it will make it easy to keep your script short and simple with the getArray() method.

 

Would you mind describing which method are you currently using that is proving to be weirdly frustrating?

 

If you can, please share the script that you're using.

 

 

 

 

 

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 04, 2021 Apr 04, 2021

Copy link to clipboard

Copied

So just to provide some context, I am incredibly new to using javascript especially within acrobat so I have an exceptionally vague notion of what the getArray does, but not a very clear one, and I don't really get the parent child hierarchy. Here is what I am using. It is a modification of what someone else had helped me with to do a similar yet different thing. Clearly my modifying of it broke the thing. 

 

var total = 0;
for ( var i=1; i<=5; i++)
{

var riskFactor = this.getField("probRiskFactor"+i+"a").value;

if(this.getField("probRiskFactor"+i+"a").valueAsString != "Off")
total+riskFactor;
}
if(total == 0)
event.value = "";
else
event.value = riskFactor;

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 04, 2021 Apr 04, 2021

Copy link to clipboard

Copied

Use this:

if(total == 0) event.value = "";

else if (total > 10) event.value = 10;

else event.value = total;

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 05, 2021 Apr 05, 2021

Copy link to clipboard

Copied

Awesome, this worked, thank you so much.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Apr 04, 2021 Apr 04, 2021

Copy link to clipboard

Copied

Use what Bernd suggested, just fix mistype ( If to if) and also change:

total+riskFactor

to:

total+=riskFactor

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 05, 2021 Apr 05, 2021

Copy link to clipboard

Copied

That worked, thank you for helping me again haha. 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines