Skip to main content
dmenebroeker_dm
Participant
June 8, 2018
解決済み

Having Javascript issues for custom calculation (unchecked Checkboxes exporting value)

  • June 8, 2018
  • 返信数 3.
  • 943 ビュー

So right now, I'm trying to create a fillable pdf form in Adobe Acrobat Pro 2017 for the character sheet of a tabletop game so it automatically calculates the various stats. The problem I'm having is that I can't get a checkbox to export the value "Off" when it's unchecked like it's supposed to. Either that or my Javascript is garbage because I don't get Javascript at all.

What I want to happen is, if the checkbox is checked AND the value in textbox called "Ranks" is 1 or greater, then the text box called "Total" is equal to the sum of 3 + ranks + other boxes. Otherwise, if the checkbox is unchecked, regardless of the value of ranks, then the total should just be the ranks and other boxes.

Right now, the value of the total box is calculating as if the checkbox were always checked

Here's the current script as is for one of the total boxes:

var skill = +getField("Skill Check").value; //this is the checkbox

var ability = +getField ("Abl Modifier").value;

var rank = +getField("Ranks 1").value;

var misc = +getField("Misc Mod. 1").value;

if (skill="Yes" && rank>0) event.value = Math.floor(3+ability+rank+misc);

else if (skill="Off") event.value = Math.floor(ability+rank+misc)

このトピックへの返信は締め切られました。
解決に役立った回答 try67

On top of what was already mentioned you have another issue.

Why did you put a "+" symbol in this line?

var skill = +getField("Skill Check").value;

Do you know what that does? It converts the value to a number, but the value of that check-box is either "Off" or "Yes", so that will produce an error, as those values can't be converted to a number. As a result the value of the "skill" variable will be "NaN" (Not a Number). You need to remove that symbol from this line.

返信数 3

try67
Community Expert
try67Community Expert解決!
Community Expert
June 8, 2018

On top of what was already mentioned you have another issue.

Why did you put a "+" symbol in this line?

var skill = +getField("Skill Check").value;

Do you know what that does? It converts the value to a number, but the value of that check-box is either "Off" or "Yes", so that will produce an error, as those values can't be converted to a number. As a result the value of the "skill" variable will be "NaN" (Not a Number). You need to remove that symbol from this line.

dmenebroeker_dm
Participant
June 13, 2018

Ah. Thank you! That fixed it. Like I said, I'm not too familiar with Javascript; I've been sort of learning on my own by googling basic Javascript functions. I also placed the double ='s just to be safe.

BarlaeDC
Community Expert
Community Expert
June 8, 2018

HI,

First point, the if statements don't appear to be correct ( based on the code above). The should have double equals rather than single

if ( skill == "Yes" && rank > 0) .....

else if ( skill == "Off" ) ......

And based on your text you want rank to be greater than 1 so it would probably need to be

if ( skill == "Yes" && rank > 1) .....

else if ( skill == "Off") ......

Hope that helps

Malcolm