8 Replies Latest reply on Oct 10, 2015 1:19 PM by gkaiseril

    Turning on/off radio buttons based on a value in a text field. How?

    Charlie6067 Level 1

      I just reviewed at least 100 topics on radio buttons and all required the user to first check a radio button. My question is different.

       

      A text field called Score displays a calculated value from 0 - 9.

       

      A group of 3 radio button are on the form and named Risk. Based on the value of the Score text field, one of the three buttons needs to turn on automatically. I know the answer is simple but I can't make the next step and need your help.

       

      Risk radio button #1 - Turn on if Score is 0-3

      Risk radio button #2 - Turn on if Score is 4-5

      Risk radio button #3 - Turn on if Score is 6-9

       

      So far I've written this to get the field value:

       

      var v = this.getField("Score");

       

      What additional JavaScript is required to check the Score value and turn on or off the radio buttons

       

      Thank you for any help you can offer.

      Charlie

      charlie6067

        • 1. Re: Turning on/off radio buttons based on a value in a text field. How?
          brew9

          Open you pdf file, get into the "Prepare Form", right click on the "Score" text field,  open the "Properties...", then like this picture,click the "Edit...", and input the script below.1.png

           

          if ( !event.willCommit && (event.change != "") )

          {

              var temp = event.change;

              var f = this.getField("Risk");

              //app.alert(g.name);

              if (temp>=0 && temp <=3){

                  var g = this.getField(f.name+".0");

                  g.defaultIsChecked(0,true);

              }

              if (temp>=4 && temp <=5){

                  var g = this.getField(f.name+".1");

                  g.defaultIsChecked(0,true);

              }

              if (temp>=6 && temp <=9){

                  var g = this.getField(f.name+".2");

                  g.defaultIsChecked(0,true);

              }

              this.resetForm(["Risk"]);

          }

          • 2. Re: Turning on/off radio buttons based on a value in a text field. How?
            Charlie6067 Level 1

            Brew9 - Many thanks for your detailed help. I've tried to work through the code with what I know but can not get the radio buttons to auto select based on the value in the Total Score text field. When I select the check boxes worth 1 point, the Total Score field tabulates the sum, but one of the three radio buttons will not select based on the Total Score field. What is missing before the radio buttons start auto filling? The three radio buttons are all named "Risk."

             

            It seems the forum does not allow attaching docs to questions or I'd attach the PDF. Have attached a couple screen shots of the form (I didn't design it )

             

            Thanks for your help with my problem,

            Best regards,

            Charlie

             

            Normal view of form

            form reg mode.png

            Form edit mode

            PDF form.png

            • 3. Re: Turning on/off radio buttons based on a value in a text field. How?
              brew9 Level 1

              If it's possible. You can email the file to me. I will finish it. The email is brew9@163.com.

              • 4. Re: Turning on/off radio buttons based on a value in a text field. How?
                brew9 Level 1

                Now I think that I don't need you file. Follow me.

                1. First, to ensure the file there is no other "Check box", then, set each "Check box", the "Export Value" to 1 for "(1 point)" and 0 for "(0 point)";

                2. Set the "Total Score" field "Read Only";

                3. According to the picture set JS for each "Check box",the code is "cal();";

                4. In the "Document JavaScripts",create a function named "cal",input the code below.

                2.png

                3.png

                function cal()

                {

                  var temp = 0;

                  for(var i = 0; i < this.numFields; i++)

                  {

                    var name = this.getNthFieldName(i);

                    var field = this.getField(name);

                    if (field.type == "checkbox" && field.value != "Off")

                    {

                      temp += field.value;

                    }

                  }

                 

                  this.getField("Total Score").value = temp;

                 

                  var f = this.getField("Risk");

                  if (temp>=0 && temp <=3){

                      var g = this.getField(f.name+".0");

                      g.defaultIsChecked(0,true);

                  }

                  if (temp>=4 && temp <=5){

                      var g = this.getField(f.name+".1");

                      g.defaultIsChecked(0,true);

                  }

                  if (temp>=6 && temp <=9){

                      var g = this.getField(f.name+".2");

                      g.defaultIsChecked(0,true);

                  }

                  this.resetForm(["Risk"]);

                }

                • 5. Re: Turning on/off radio buttons based on a value in a text field. How?
                  Charlie6067 Level 1

                  Hi Brew9 - many thanks for your help with this. I was able add the script and the radio buttons are firing according to the total score. I am in your debt! Thank you for sharing your expertise. Do you work specifically with PDFs or can you apply your broad JavaScript knowledge to PDFs? I wish there was a training program for JavaScript for PDFs.

                   

                  Thanks much!

                  Charlie

                  • 6. Re: Turning on/off radio buttons based on a value in a text field. How?
                    JoelGeraci_Datalogics Level 3

                    Charlie:

                     

                     

                    I wish there was a training program for JavaScript for PDFs.

                     

                     

                    There is... pdfscripting.com

                     

                    J-

                    • 7. Re: Turning on/off radio buttons based on a value in a text field. How?
                      brew9 Level 1

                      Hi, Charlie6067

                      I'm sorry I can't reply you in time. PDF is not my career, but I knew a little javascript.

                      I have read the "js_developer_guide.pdf" and "js_api_reference.pdf" of Adobe Acrobat DC When I need to make a PDF form file in my work. so I can do something.

                      I happen to know your question, do not mind it too much.

                      By the way, I'm from Chinese.

                      • 8. Re: Turning on/off radio buttons based on a value in a text field. How?
                        gkaiseril MVP & Adobe Community Professional

                        Acrobat's JavaScript is the same as the Core for web JavaScript except for the special PDF extension items, properties, and methods. So if one learns Acrobat JavaScript much of the code could easily be used in web pages or other variations of JavaScript.