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

Acrobat forms if radio buttons and checkboxes checked

Explorer ,
Jun 23, 2022 Jun 23, 2022

Hi everybody,

I'm trying to create an acrobat form for the first time with some JS code. I'm trying since two days now, but somehow cannot figure out how to check if radio buttons and checkboxes are checked and then do a specific calculation.

Acrobat calculation.pngexpand image

The form looks like this. In the last field should come a calculation with the values of the fields above. So for example if radio option 2 and first checkbox are check the total value is 60, so in the bottom field has to be "9" (15% of 60).

 

Hope somebody here can help me out with this.

 

Appreciate any help or advice you could give.

 

Thanks a lot in advance!

TOPICS
How to , JavaScript , PDF forms
5.6K
Translate
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 ,
Jun 24, 2022 Jun 24, 2022

my idea for checkboxes is something like this:

 

if (CheckBox1 selected) event.value="Checkbox is selected" 

and in the field in front would be the same

if (CheckBox1 selected) event.value=CheckBox1

 

Possible like that? How would the correct syntax be? And how to achieve this for the radio buttons? 

 

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

I meant what are field names where you want to show "description" and value of radio/checkbox?

Why you have Radio1, Radio2, Radio3, you don't use them as a group?

Is empty row for checkbox 2?

Anyway, assuming radio buttons are actually group called "Radio" use this as custom calculation script of one of the fields in first row and change "Text1","Text2" to those names in first row, you can apply same principle for checkbox fields:

var a1 = this.getField("Text1");
var a2 = this.getField("Text2");
var rb = this.getField("Radio").valueAsString;

if(rb != "Off"){
if(rb == "10"){
a1.value = "Radio Option 1";
a2.value = 10;}
else if(rb == "20"){
a1.value = "Radio Option 2";
a2.value = 20;}
else if(rb == "30"){
a1.value = "Radio Option 3";
a2.value = 30;}}
else{
a1.value = "";
a2.value = "";}

 

 

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

OK, I'll try that one!

 

But first, I think I did the checkbox-thing all by myself 🙂 🙂 hope it's right this way:

For the checkbox I inserted this script for mouse-up action:

 

if ( event.target.isBoxChecked(0)) {
this.getField("CheckBox1_option").value = "This is Checkbox 1";
this.getField("CheckBox1_option_price").value = "CHF 40";
}else {
this.getField("CheckBox1_option").value = "";
this.getField("CheckBox1_option_price").value = "";

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

Honestly I did not understand that code, so I tried another way. For each of the radio buttons I inserted these code as mouse up event:

For first Radio Button

if ( event.target.isBoxChecked(0)) {
this.getField("Summary_Radio").value = "Radio button 1 is selected";
this.getField("Summary_Radio_price").value = 10;
}else {
this.getField("Summary_Radio").value = "";
this.getField("Summary_Radio_price").value = "";
}

 

For second Radio Button

if ( event.target.isBoxChecked(0)) {
this.getField("Summary_Radio").value = "Radio button 2 is selected";
this.getField("Summary_Radio_price").value = 20;
}else {
this.getField("Summary_Radio").value = "";
this.getField("Summary_Radio_price").value = "";
}

 

For third Radio Button

if ( event.target.isBoxChecked(0)) {
this.getField("Summary_Radio").value = "Radio button 3 is selected";
this.getField("Summary_Radio_price").value = 30;
}else {
this.getField("Summary_Radio").value = "";
this.getField("Summary_Radio_price").value = "";
}

 

 

 

The first one works good, but the second and third don't work at all. Any idea about that?

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

With

 event.target.isBoxChecked(0)

you test always the first radio button.

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

Instead of event.target.isBoxChecked(0) use: event.target.value != "Off".

'else' part won't do anything since you can't uncheck radio button.

It would be better if you used calculation script in one of those text fields.

What are choices for radio buttons?

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

Thanks! Now I saw that I need to be able to also uncheck the radio buttons, so i changed from radio button to checkbox, named them all the same and it works. So these three top checkboxes are separate from the others and work as radio buttons, so for better understanding I still call them Radio. For example now i have the first one (value 10) selected and then also checked some of the other checkboxes below, let's say value 40 and 50. Now if I change my mind and select the third Radio (value 30), I want the two below checkboxes (40 and 50) get unchecked. How can I achieve that?

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

In script of "Radio" checkboxes add code to uncheck those two checkboxes:

if ( event.target.isBoxChecked(0)) {
this.getField("Summary_Radio").value = "Radio button 3 is selected";
this.getField("Summary_Radio_price").value = 30;

this.getField("Checkbox 40").value = "Off";

this.getField("Checkbox 50").value = "Off";}

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

Did that and works, thanks!

 

Now it seems I have a bug in the add-calculation on the second page.

 

I inserted the blue text-field for testing only, that's why it is on the side. Looks like this now:

Acrobat calculation error.pngexpand image

the code of the blue field is this:

event.value =
(this.getField("Radio_option_price").value) +

(this.getField("CheckBox1_option_price").value) +

(this.getField("CheckBox2_option_price").value) +

(this.getField("ChcekBox3_option_price").value) +

(this.getField("RadioExpress_option_price").value)

 

In this example there are only values for "Radio_option_price" and "CheckBox3_option_price". All fields are formated as numbers, currency CHF.

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

it's super weird! 

When I have CheckBox1 checked and then also check CheckBox2, everything is right, calculation is correct! But when I don't have CheckBox1 checked and directly check CheckBox2, it goes wrong! It somehow does not recognize the numbers as numbers (I think). And in any case, if I check CheckBox3 also, it goes wrong anyway! Super crazy!!! Any idea about that?

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

It concatenes the text strings. Use the number values.

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

Yeah, this is what i thought also, but how to do that? I checked again, all fields are formated as numbers, so the values should be numbers, right?? Is there a way to make sure I get them as numbers and not as text string?

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

E.g.:

+this.getField("ChcekBox3_option_price").value

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

this is exactly how I have it, right? Only I had brackets for each. Removed these brackets, although I didn't think it would make any difference. And yes, did not make any difference, still the same 😞

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

You don't have the same.

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

I don't?

(this.getField("ChcekBox3_option_price").value) +

so without the brackets now it is

this.getField("ChcekBox3_option_price").value +

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

for me it looks quite the same, doesn't it?

 

There is a small typo error here CheckBox/ChcekBox.... but I have it correctly in my real file.

Translate
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 ,
Jun 24, 2022 Jun 24, 2022

Add + before this

Translate
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 ,
Jun 25, 2022 Jun 25, 2022
LATEST

Thanks a lot everybody for all your help and support! Finally the form is working as it should and I learned a lot doing that! I really love a community and helpful people like that! Thank you!!

Translate
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