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

Making two dropdown lists dependent on exported value

Community Beginner ,
May 21, 2020 May 21, 2020

Hi,

I'm very new to working with forms and javascript so please bear with me.

 

I've got two dropdown lists with export values used for simple calculation. (i.e.:  event.target.value = this.getField("12Round").value; in a separate text box)

Both of the dropdown lists have three options: None, One, and Two with export values of 0, 2, and 4 respectively.

 

The issue I have is that both are dependent on the other.

Example:  Take a box with two lids that can have two different sets of openings; Round or Square. When the dropdown choice comes up, the quantity outputs for each field work fine, however (2) of each dropdown can't be chosen.  Conditions that work are: (1) of either type, (2) of one type, or (1) of each.

 

Is there a way to have the dropdown box selections (freeze/ change colour?) as soon as the maximum is reached between both of them?

 

Thanks!!

TOPICS
Acrobat SDK and JavaScript
1.4K
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

correct answers 2 Correct answers

Community Expert , May 21, 2020 May 21, 2020

Yes, use the calculation script in the text field to make the dropdowns ReadOnly when the max is reached.

 

var nSum = this.getField("12Round").value + this.getField("Other dropdown").value;

if(nSum > nMax)

{

     this.getField("12Round").readOnly = true;

     this.getField("Other dropdown").readOnly = true;

 

 

Translate
Community Expert , May 27, 2020 May 27, 2020

Use readonly not readOnly

Translate
Community Expert ,
May 21, 2020 May 21, 2020

Yes, use the calculation script in the text field to make the dropdowns ReadOnly when the max is reached.

 

var nSum = this.getField("12Round").value + this.getField("Other dropdown").value;

if(nSum > nMax)

{

     this.getField("12Round").readOnly = true;

     this.getField("Other dropdown").readOnly = true;

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
May 25, 2020 May 25, 2020

This code is missing the definition of the nMax variable.

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 ,
May 25, 2020 May 25, 2020

Also, fields don't have a "readOnly" property. That's for annotations.

For fields it is called "readonly". Consistency is boring, after all...

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 ,
May 25, 2020 May 25, 2020

Sorry to nitpick but this code is also missing a condition to make the fields editable back again. Otherwise, once they become disabled they will always remain that way.

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 Beginner ,
May 25, 2020 May 25, 2020

Hi try67,

What suggestions do you have, based on what I need to be (able) to do with them?  Or is there another way to make the optional choices work together?

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 Beginner ,
May 25, 2020 May 25, 2020

Hi,

Unfortunately this didn't work on my form. Not sure what was causing two things to happen... first, both fields were still allowing the total values to show; dropdown not becoming a read-only. Then the formula wasn't allowing any calculation to happen.

 

See my attached .jpg and let me know if I'm missing something else, or have the formula entered incorrectly.

 

Thanks!form option and script entryform option and script entryexpand image

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 ,
May 25, 2020 May 25, 2020

Where have you nMax defined?

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 Beginner ,
May 25, 2020 May 25, 2020

Hi Bernd_Alheit,

As try67 noted, it's not defined anywhere... at this point.

 

I figured it needed to be as the function needed to know what that (max) would be.  I tried putting (something) like var nMax = 4 at the beginning  (not knowing where it should be or how it should read, of course)... and nothing.

 

Help!

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 ,
May 25, 2020 May 25, 2020

Check the Javascript console for errors.

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 Beginner ,
May 25, 2020 May 25, 2020

I just updated Java (yes it's been a while since the last update).  However if there happened to be a javascript console error, I'm not seeing how this affects a .pdf (form)... sorry extreme newbie here.

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 ,
May 25, 2020 May 25, 2020

In Acrobat open the Javascript console with ctrl-j

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 Beginner ,
May 25, 2020 May 25, 2020

(Finally figured out how the console monitors the form)

I could only see it was catching no maximum for "nMax".  Is "n" the max value, or is there something else I'm missing?

 

I've tried looking up "nMax" and cannot find any reference as to where/ how I'd set what that value would be.

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 ,
May 25, 2020 May 25, 2020

Add

var nMax = 4;

to the begin of the code.

 

 

 

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 Beginner ,
May 27, 2020 May 27, 2020

Hi Bernd_Alheit,

Unfortunately adding that line to the custom calculation script, no matter which line I put it on, doesn't change the result.  Here's the current script I have as custom calculation for the text field and I know the first line is the only one getting a value. I've tried putting this in as a validation script but same result... the nSum > nMax don't override.

 

Attached is a sample of how my spreadsheet responds when incorrect values are chosen.  Would a List choice option work better for this script?

Spreadsheet formulas.jpgexpand image

 

event.target.value = this.getField("12Round").value;

var nSum = this.getField("12Round").value + this.getField("Bear").value;
var nMax = 4;
if(nSum > nMax)

{

this.getField("12Round").readOnly = true;
this.getField("Bear").readOnly = true;

}

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 ,
May 27, 2020 May 27, 2020

Use readonly not readOnly

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 Beginner ,
Jun 05, 2020 Jun 05, 2020

Thanks Bernd-Alheit, one letter made the difference!

 

On that note, to allow the user to correct the form rather than having it suddenly become a read-only form, I added a different outcome for the condition.  It works, to a point by changing the field colours to red and then back to black if corrected.  However, it seems to change alternate fields when the error happens.

 

 

event.target.value = this.getField("Bear").value;

var nSum = this.getField("12Round").value + this.getField("Bear").value;
var nMax = 4;

if(nSum > nMax)

{

this.getField("R4BQ" && "R4RQ").textColor = color.red;

}

else
this.getField("R4BQ" && "R4RQ").textColor = color.black;

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 05, 2020 Jun 05, 2020

This is not how you access multiple fields.

Change:

this.getField("R4BQ" && "R4RQ").textColor = color.red;

To:

this.getField("R4BQ").textColor = color.red;

this.getField("R4RQ").textColor = color.red;

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 Beginner ,
Jun 05, 2020 Jun 05, 2020
LATEST

Thanks try67.

 

I changed the calculation script in both fields, however it only recognizes the first line regardless of which drop down produces the error.  Basically the first "this.getField" row ("R4BQ") is the only one that changes colour.  Is there another way to ensure both will turn red when the error occurs? 

 

Or does the order of each (field) play a roll on the outcome, depending on the text box it's in?

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