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

Force value using checkbox

Community Beginner ,
Apr 18, 2019 Apr 18, 2019

Hoping someone can help with this.

I have a form with several rows of fields as follows (only showing row 1 below)...

"Unit_1" (dropdown), "Unit_1_Fee" (textbox, currency), "Unit_1_Var" (textbox, numeric), "Unit_1_Actual_Fee" (textbox, currency).

When a unit is selected in "Unit_1", the associated fee is shown in "Unit_1_Fee" which is a value retrieved from a Document JavaScript.

If "Unit_1_Var" is empty, the value in "Unit_1_Fee" is also displayed in "Unit_1_Actual_Fee".

If a "1" is entered into "Unit_1_Var", the value in "Unit_1_Actual_Fee" is "120" (an override value).

The following script is attached to the "Unit_1_Actual_Fee" field to achieve this...

if (this.getField("Unit_1").valueAsString!=this.getField("Unit_1").defaultValue) {

var altValue = Number(this.getField("Unit_1_Var").value) * 120;

    event.value = altValue>0 ? altValue : (Number(this.getField("Unit_1_Fee").value);

} else event.value = "";

What I'm hoping to do is to modify this to use a checkbox instead of the numeric field "Unit_1_Var".

Ideally, I would like to be able to get rid of the "Unit_1_Actual_Fee" field and just have the "Unit_1_Fee" value change from the lookup value to "120" if the checkbox is selected.

Hopefully this makes sense. Any help would be appreciated.

TOPICS
Acrobat SDK and JavaScript , Windows
2.0K
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 1 Correct answer

Community Expert , Apr 18, 2019 Apr 18, 2019

The "if" block I supplied only deals with the checkbox. So yes, some of your other code needs to be copied over to get the full effect.

Like this:

if (this.getField("Unit_1").valueAsString!=this.getField("Unit_1").defaultValue)

{

     if(this.getField("Unit_1_Check").value == "Off")

      {

           normal calculation

      }

      else

           event.value = 120;

}

else

      event.value = "";

Translate
Community Expert ,
Apr 18, 2019 Apr 18, 2019

The above code should work with a check-box field as well as a text field, since the default value of a check-box is (usually) "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
Community Beginner ,
Apr 18, 2019 Apr 18, 2019

Can't seem to work this out. I'm a bit of a hack when it comes to coding, but I'm trying a few variations. 🙂

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 ,
Apr 18, 2019 Apr 18, 2019

Let's say the check box is named "Unit_1_Check". This could be your calculation code for "Unit_1_Fee".

if(this.getField("Unit_1_Check").value == "Off")

{

    normal calculation

}

else

   event.value = 120;

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 Beginner ,
Apr 18, 2019 Apr 18, 2019

Thanks Thom. This works OK but the "Unit_1_Fee" remains populated with 120 even if there is no unit selected in "Unit_1".

I assume I need another line to check that the "Unit_1" field is populated, otherwise return a blank value in "Unit_1_Fee".

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 ,
Apr 18, 2019 Apr 18, 2019

The "if" block I supplied only deals with the checkbox. So yes, some of your other code needs to be copied over to get the full effect.

Like this:

if (this.getField("Unit_1").valueAsString!=this.getField("Unit_1").defaultValue)

{

     if(this.getField("Unit_1_Check").value == "Off")

      {

           normal calculation

      }

      else

           event.value = 120;

}

else

      event.value = "";

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 Beginner ,
Apr 19, 2019 Apr 19, 2019

Works perfect. I was missing the second 'else'.

Thanks. 🙂

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 ,
Apr 19, 2019 Apr 19, 2019

Just found a problem that wasn't happening before.

I have a "Total_Fee" field at the bottom of the Fee column that sums the Fee fields (there are 6 of them).

Here's the code...

event.value = Number(this.getField("Unit_1_Fee").value) + Number(this.getField("Unit_2_Fee").value) + Number(this.getField("Unit_3_Fee").value) + Number(this.getField("Unit_4_Fee").value) + Number(this.getField("Unit_5_Fee").value) + Number(this.getField("Unit_6_Fee").value);

The Total_Fee updates OK when rows 1 and 2 are populated, but if any of the other rows have a unit added the Total_Fee doesn't update straight away but is delayed by one operation. When I enter Unit 3 the total fee doesn't change even though the "Unit_3_Fee" field is updated. Then when I enter Unit 4 the total fee updates to what it should have been after adding Unit 3.

This behavior continues as I add and delete units, or check and uncheck the check boxes. Updating of the total fee is always delayed by one operation.

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 ,
Apr 19, 2019 Apr 19, 2019

This is typical of an incorrect field calcultion order. You can fix it in Prepare Form mode by going to More - Set Field Calculation Order and moving the total field down the list so that it is calculated after the fields it is dependent on.

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 ,
Apr 19, 2019 Apr 19, 2019
LATEST

That fixed it. I have a vague recollection of you telling me about that little trap in a previous post about 2 years ago.

Thanks for the reminder, and for your patience with those of us not as proficient with JavaScript.

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