Solved

JavaScript in Adobe Acrobat

Posted on 2012-03-13
7
434 Views
Last Modified: 2012-06-22
I have a series of check boxes, 5 of them to be specific.  I then have a radio button for member or non member and one more check box for additional service fee.  I then have a total text box.

What I would like to do is have the script calculate the total off of the radio button value they are set to 110 and 130.  then multiply the number of check boxes checked by 50$ and add the additional service fee to the total if checked.  I would like this amount to show up in the Total value text box.

I am not sure how to do the radio buttons. would I do 2 if statements? one for if(this.getField("Naturalist_Membership").value == "110" then ... and one for if (this.getField("Naturalist_Membership").value == "130" then ...?
0
Comment
Question by:lsctech
  • 4
  • 3
7 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 37719678
I would use the following as the calculation script for the result text box:

// Count the checked check boxes
var CB_Count = 0;
var baseName = "CB.";
for (var i=0; i<5; i++)
{
    if (this.getField(baseName + i).value == "Yes")
        CB_Count++;
}

// get the fee
var fee = this.getField("Naturalist_Membership").value;

// store the result in this text box
event.value = (CB_Count * 50) + fee;
0
 

Author Comment

by:lsctech
ID: 37725674
Here is my exact code. I will also attach my PDF version.  I can not get it to display any text in the textbox. Please help! Thanks.

// Count the checked check boxes
var CB_Count = 0;
var baseName = "yn_";
for (var i=0; i<9; i++)
{
    if (this.getField(baseName + i).value == "Yes")
        CB_Count++;
}

// get the fee
var fee = this.getField("Naturalist_Membership").value;
var extra_fee
if (this.getField("yn_extended").value == "Yes")
  extra_fee = 25;
 
// store the result in this text box
event.value = (CB_Count * 50) + fee + extra_fee;
BHNPsummer-camps-registration-20.pdf
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 500 total points
ID: 37725815
There are a couple of things that are not correct with your code:

1. Your fields are named yn_1..yn_10, but your loop goes over the fields yn_0..yn_9
2. You must have changed the export value for the checkboxes - the default is "Yes", but you are using "On". You need to change the script to reflect that as well.
3. I would initialize the variable extra_fee to 0 (and the line needs a ";" at the end).

Try this:

// Count the checked check boxes
var CB_Count = 0;
var baseName = "yn_";
for (var i=1; i<10; i++)
{
    if (this.getField(baseName + i).value == "On")
        CB_Count++;
}

// get the fee
var fee = this.getField("Naturalist_Membership").value;
var extra_fee = 0;
if (this.getField("yn_extended").value == "On")
  extra_fee = 25;
 
// store the result in this text box
event.value = (CB_Count * 50) + fee + extra_fee;
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:lsctech
ID: 37726072
Ok awesome that works! the only thing is how do I get it not to say 0Off0 in the total text box.  I would be ok with it just saying 0
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 37726528
Ah... I see the same behavior here. Something must have happened when I copied/pasted the code. I am looking into it.
0
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 500 total points
ID: 37726564
Found it...

You do not have a default state for the radio button. I assume it's wrong to not have anything selected for the membership level, so I would just define a default state (e.g. non-member) and that will take care of this problem. What you are seeing is that in the case of no selected option, the control returns "Off" instead of a value. If you do want the current behavior, just add the following like after you determine the fee:

if (fee == "Off")
    fee = 0;

This will force fee to a zero, and the total will be correct.

As I said, I would change the behavior of the radio button. If you still want "0" to be displayed when nothing is selected, just make a final check after you calculate the total:

var total =  (CB_Count * 50) + fee + extra_fee;
if (CB_Count == 0)
    total = 0;
event.value = total;
0
 

Author Closing Comment

by:lsctech
ID: 37726578
Thank you for your help!! that did it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 36
Regular Expression Calculator Tester 2 57
is this a cms? 8 34
Wrapper for APPs 9 35
PDF files have been in the limelight due to its unmatched features.  Personal documents, emails, business reports and eBooks are all converted into PDF files owing to peerless features provided by it. Adding watermark to a PDF file is a method to se…
This article discusses how to create an extensible mechanism for linked drop downs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now