Solved

JavaScript in Adobe Acrobat

Posted on 2012-03-13
7
433 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
XSS cleaning when using ajax & json encode 5 38
Need to learn promise API 2 28
Phone Dialer 5 36
Different Delete Messages 7 17
Inserting page numbers in Portable Document Files not only enhances manageability but also makes them look professional. With numbered pages, the file appears more organized and it becomes easier to search for a particular page. The size and the vol…
This article discusses how to create an extensible mechanism for linked drop downs.
In this first video of the three-part Xpdf series, we introduce and describe Xpdf, a library containing nine command line utilities that perform various functions on PDF files. We show where the library is located and how to download it, discuss its…
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…

758 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

20 Experts available now in Live!

Get 1:1 Help Now