Solved

JavaScript in Adobe Acrobat

Posted on 2012-03-13
7
437 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is ā€“ how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a cā€¦

730 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