Skip to main content
Participant
January 1, 2025
質問

Need help in a form to calculate the value of a field based on the visibility of radio buttons

  • January 1, 2025
  • 返信数 1.
  • 677 ビュー

I'm creating a sheet for a game.  I have a list of skills with 3 radio buttons stacked on top of each other next to each skill.  The idea is that if you do not have that skill, the "Blank" radio button is visible.  If you have partial skill in it, the "Partial" button (which is a half circle image) is visible, and if you are fully skilled, the "Skilled" button is visible (full circle image).  When one is visible, the other two are hidden. I am using Run Javascript on Mouse Up as the action for each button to hide/show each one.  That script is very simple and looks like this:


this.getField("PartialSkillLift").display = display.visible;

this.getField("SkilledLift").display = display.hidden ;

this.getField("BlankLift").display = display.hidden ;

 

That part is working fine, but what i would like to add to it that i can't seem to get figured out is i would like the skill to show a number or do a calculation based on which button is showing.  So, if the Blank button is visible, it just displays the number from a stat bonus (i.e. "StrengthBonus" field). If the Partial button is shown, it does the calculation of "StrengthBonus" + 1).  And if the Skilled button is shown, it does the calculation "StrengthBonus + Level + 3" ("Level" is also a field already set up).

 

I have this in the "Lift" field currently...

 

strbonus = this.getField("StrengthBonus").value;
level = this.getField("Level").value;
skilllift = this.getField("SkilledLift").display = display.visible;
blanklift = this.getField("BlankLift").display = display.visible;
partlift = this.getField("PartialSkillLift").display = display.visible;
 
 
if (strbonus !=="" && skilllift !=="" && level !=="")
{
this.getField("Lift").value = strbonus + level + 3;
}
else if (strbonus !=="" && blanklift !=="")
{
this.getField("Lift").value = strbonus;
}
else if (strbonus !=="" && partlift !=="")
{
this.getField("Lift").value = strbonus + 1;
}
else event.value = "";
 
Which, when the strbonus value is entered, that number populates correctly in the "Lift" field, and once you add a value to the "Level" field, it correctly does the math (strbonus + level +3) and displays it in the "Level" field....
A couple of issues though are that all of the button fields are just always visible (which is not intended), and clicking throught the buttons doesn't actually have any effect on the values at all.  The math that is working doesn't seem to be tied to the button presses at all...it's just doing the math options for when the two fields that feed it are entered. 
 
I'm really new to javascript, so i've pretty much just been trying to teach myself this as i go through google searches and videos.  I've had a lot of success at the rest of this form, but this last piece really has me stumped.  Any assistance i can get would be greatly appreciated.

 

返信数 1

PDF Automation Station
Community Expert
Community Expert
January 2, 2025

"all of the button fields are just always visible "  That's because these lines of code are setting them to visible:

skilllift = this.getField("SkilledLift").display = display.visible;
blanklift = this.getField("BlankLift").display = display.visible;
partlift = this.getField("PartialSkillLift").display = display.visible;
Also, the statements
skilllift !=="" 
blanklift !==""
partlift !==""
will always return true because you are setting their values to 0 with the previous 3 lines I listed.
Tip:  You don't need 3 buttons with show/hide to get your 3 images.  You can use one button that changes the icon whenever it is clicked.  Read this article:
Then store a value for each icon displayed in the button that changes when the button is clicked and use that value in your calculations.
Chazwiz作成者
Participant
January 3, 2025

Thank you so much for the assistance with the carousel button.  That is SUPER nice, to only have one button for each skill instead of three for each.  Way cleaner.  I haven't had a chance to have the new buttons values start feeding the Skill yet, but thats my next task.  Was just really excited to find out that the buttons could carousel in that way though.  Very helpful, and i appreciate the reply!

PDF Automation Station
Community Expert
Community Expert
January 4, 2025

So, i have gone into the field that the carousel button was supposed to feed values into and entered some script.  I'm not sure what i'm doing incorrectly.  When the "strbonus" field has a number entered into it, the script is populating into the "Lift" field the way it should (especially since the buttons default state is "Blankdot")...but when i cycle through the other button presses...it's not changing the values the way it should.  I'm obviously missing something in my script that is needed, i just don't know what it is.  Below is the script i have entered into the "Lift" field:

 

strbonus = this.getField("StrengthBonus").value;
level = this.getField("Level").value;
 
 
if (event.target.userName="Blankdot")
{
this.getField("Lift").value = strbonus;
}else
if (event.target.userName="Partdot")
{
this.getField("Lift").value = strbonus + 1;
}else
if (event.target.userName="Skilldot")
{
this.getField("Lift").value = strbonus + level + 3;
}

 


As @Nesa Nurani said, you need 2 equal signs for each comparison.  You also need to name the button field instead of event.target, which is the Lift field in this case.

if(this.getField("ButtonFieldName").userName=="Blankdot")

Replace "ButtonFieldName" with the actual name of the field.