• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Calculations Using Radio Buttons

New Here ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

Hello, 

I am very new to javascript and am struggling to calculate a this function. I have 2 sets of ratio buttons that are associated with a cost. I would like to muiltiply the users choice by a quanity that they input and then add those 2 values together. i.e: 

button group 1 - ()$2 a week or ()$7 a month. Qt needed=QT1

button group 2 - ()$9 a week or ()$33 a month. Qt needed =QT2

 

In properties I set my radio button values as 2, 7, 9, and 33 respectively. I have the following in my calculation but "NaN" pops up as my event value instead of a number:

var SM = this.getField("button group1").value;

var ST = getField("QT1").valueAsString;

     if(SM == 'OFF') FieldS = 0;

     else (SM == 'ON') FieldS = SM * ST;

var ME = this.getField("button group 2").value;

var MT = getField("QT2").valueAsString;

     if(ME == 'OFF') FieldM = 0;

     else (ME == 'ON') FieldM = ME * MT;

event.value = FieldS + FieldM;

 

Any help would be appreciated! 

Thanks 🙂 

TOPICS
How to , JavaScript , PDF forms

Views

218

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
Community Expert ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

Set the format of the field to "None" and look at the result.

Votes

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
Community Expert ,
Apr 15, 2021 Apr 15, 2021

Copy link to clipboard

Copied

Try this:

var total = 0;
var g1 = this.getField("button group1");
var g2 = this.getField("button group2");
var qt = this.getField("QT1");

if(g1.valueAsString != "Off")
total = Number(g1.value)*Number(qt.value);
if(g2.valueAsString != "Off")
total += Number(g2.value)*Number(qt.value);
if(total == 0)
event.value = "";
else event.value = total;

Votes

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
Community Expert ,
Apr 16, 2021 Apr 16, 2021

Copy link to clipboard

Copied

LATEST

- The default valu of a radio-button group when nothing is selected is "Off", not "OFF".

- This is not valid code:

else (ME == 'ON') FieldM = ME * MT;

After "else" only a command should come, not a condition.
If you want to use another condition then you must use "else if".

Also, the export values of the radio-buttons are numbers, as you wrote, so why are you comparing them to "ON"?

And if they are "ON", then you can't multiply that value by a number...

Just use:

else FieldM = ME * MT;

Votes

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