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

Submit button to add +1 in form field

New Here ,
Jul 02, 2018 Jul 02, 2018

Copy link to clipboard

Copied

(EDITED to clarify question)

I have a group of radio buttons named KAQuestion.1 (export values are either 0 or 1), a Submit1 button, and two fields - CorrectTotal and IncorrectTotal.

If the radio value is 0, submit should trigger 1 to be added to IncorrectTotal

If the radio value is 1, submit should trigger 1 to be added to CorrectTotal

Please help me with the script for the Submit button.  Here's the core of what I'm looking at:

var KAQ = this.getField("KAQuestion.1").value;

if (KAQ == 0)

this.getField("IncorrectAnswerTotal").value = +1;

else

this.getField("CorrectAnswerTotal").value = +1;

I had this script as a calculation on the CorrectTotal, which works, but I don't want the calculation to trigger until the user commits to the submit button

var testFor = "1";

var groups = this.getField("KAQuestion");

var ar = groups.getArray();

var cnt = 0;

for (var i=0; i<ar.length; i++) {

    if (ar.value == testFor) {

        cnt++;

    }

}

event.value = cnt;

Thanks in advance.

TOPICS
Acrobat SDK and JavaScript , Windows

Views

783

Translate

Translate

Report

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 , Jul 03, 2018 Jul 03, 2018

Use this code:

var KAQ = Number(this.getField("KAQuestion.1").value);

if (KAQ == 0) this.getField("IncorrectAnswerTotal").value = Number(this.getField("IncorrectAnswerTotal").value)+1;

else this.getField("CorrectAnswerTotal").value = Number(this.getField("CorrectAnswerTotal").value)+1;

Votes

Translate

Translate
Community Expert ,
Jul 03, 2018 Jul 03, 2018

Copy link to clipboard

Copied

Use this code:

var KAQ = Number(this.getField("KAQuestion.1").value);

if (KAQ == 0) this.getField("IncorrectAnswerTotal").value = Number(this.getField("IncorrectAnswerTotal").value)+1;

else this.getField("CorrectAnswerTotal").value = Number(this.getField("CorrectAnswerTotal").value)+1;

Votes

Translate

Translate

Report

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
New Here ,
Jul 03, 2018 Jul 03, 2018

Copy link to clipboard

Copied

Thank you so much!  I had just tried the following (which looks close to yours without the Number() designation) before your reply:

var KAQ = this.getField("KAQuestion.1").value;

var subI = this.getField("IncorrectAnswerTotal").value;

var subC = this.getField("CorrectAnswerTotal").value;

if (KAQ == 0)

this.getField("IncorrectAnswerTotal").value = subI +1;

else

this.getField("CorrectAnswerTotal").value = subC +1;

I appreciate you taking the time to reply!

Votes

Translate

Translate

Report

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
New Here ,
Jul 04, 2018 Jul 04, 2018

Copy link to clipboard

Copied

Follow-up question... How can I make the Submit1 button readonly after being clicked so each subsequent click doesn't keep adding +1 to the Incorrect/Correct field?  I've tried adding:

event.target.readonly = true

Thank YOU!

Votes

Translate

Translate

Report

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 ,
Jul 04, 2018 Jul 04, 2018

Copy link to clipboard

Copied

That should work...

Votes

Translate

Translate

Report

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
New Here ,
Jul 04, 2018 Jul 04, 2018

Copy link to clipboard

Copied

Does now, not sure why it didn't earlier.  Thanks for the confirmation!

Votes

Translate

Translate

Report

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
New Here ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

Hi try67!  I hope that you can help me again (I'm not sure if this should be posted as a separate question), but now the radio buttons have been changed to check boxes...

I have 5 check boxes called KAQuestion7.1, 7.2, etc., as well as a Correct and Incorrect button, and a CorrectAnswerTotal and IncorrectAnswerTotal field.

If KAQuestion7.3 ONLY is checked,

1) show Correct button

2) add +1 to CorrectAnswerTotal. 

else (any other combination of checkboxes),

1) show Incorrect button

2) add +1 to IncorrectAnswerTotal. 

Here's what I'm working with:

if (this.getField("KAQuestion7.3").isBoxChecked(0)) {

  this.getField("3Correct").display = display.visible

  this.getField("3Incorrect").display = display.hidden

  this.getField("CorrectAnswerTotal").value = Number(this.getField("CorrectAnswerTotal").value)+1;

}

else if (this.getField("KAQuestion7.1").isBoxChecked(0) && this.getField("KAQuestion7.2").isBoxChecked(0) && this.getField("KAQuestion7.3").isBoxChecked(0) && this.getField("KAQuestion7.4").isBoxChecked(0)) {

  this.getField("3Incorrect").display = display.visible

  this.getField("3Correct").display = display.hidden

  this.getField("IncorrectAnswerTotal").value = Number(this.getField("IncorrectAnswerTotal").value)+1;

}

else {

  this.getField("3Incorrect").display = display.visible

  this.getField("3Correct").display = display.hidden

  this.getField("IncorrectAnswerTotal").value = Number(this.getField("IncorrectAnswerTotal").value)+1;

}

What's happening is...

1) when all check boxes are selected its giving a point to correct when it is not.  only checkbox 7.3 being checked is correct.

2) the correct/incorrect buttons aren't showing up correctly - should their individual property be set as visible or hidden?

Thank you in advance!

Votes

Translate

Translate

Report

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 ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

If nothing is checked you want to count it as incorrect?

Votes

Translate

Translate

Report

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
New Here ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

The submit button (where I am putting this code) will be readonly until they check something, so at the point the code is triggered something will be checked.

Votes

Translate

Translate

Report

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 ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

OK... Then you don't need the entire middle part of the code.

Just use this:

if (this.getField("KAQuestion7.3").isBoxChecked(0)) {

  this.getField("3Correct").display = display.visible

  this.getField("3Incorrect").display = display.hidden

  this.getField("CorrectAnswerTotal").value = Number(this.getField("CorrectAnswerTotal").value)+1;

} else {

  this.getField("3Incorrect").display = display.visible

  this.getField("3Correct").display = display.hidden

  this.getField("IncorrectAnswerTotal").value = Number(this.getField("IncorrectAnswerTotal").value)+1;

}

Votes

Translate

Translate

Report

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
New Here ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

Checking a combination that includes 7.3 makes it count as correct.  I want to allow 7.3 with other options, but it does not count as correct unless 7.3 alone is checked...

Votes

Translate

Translate

Report

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 ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

I'm having a hard time understanding the situation. Can you share the actual file?

Votes

Translate

Translate

Report

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
New Here ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

Can I share it with you directly and not publicly?

In the meantime, to clarify - It is a select all that apply question, so selecting just the third checkbox is correct.  Selecting all of them, including the third checkbox is wrong.  So, the code should return correct if only checkbox 7.3 is selected and incorrect if any other combination is selected.

Votes

Translate

Translate

Report

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 ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

You can email it to me privately to try6767 at gmail.com and I'll look into it tomorrow, but try this code first:

if (this.getField("KAQuestion7.3").isBoxChecked(0)==true && this.getField("KAQuestion7.1").isBoxChecked(0)==false && this.getField("KAQuestion7.2").isBoxChecked(0)==false && this.getField("KAQuestion7.3").isBoxChecked(0)==false && this.getField("KAQuestion7.4").isBoxChecked(0)==false) {

  this.getField("3Correct").display = display.visible

  this.getField("3Incorrect").display = display.hidden

  this.getField("CorrectAnswerTotal").value = Number(this.getField("CorrectAnswerTotal").value)+1;

} else {

  this.getField("3Incorrect").display = display.visible

  this.getField("3Correct").display = display.hidden

  this.getField("IncorrectAnswerTotal").value = Number(this.getField("IncorrectAnswerTotal").value)+1;

}

Votes

Translate

Translate

Report

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
New Here ,
Jul 14, 2018 Jul 14, 2018

Copy link to clipboard

Copied

LATEST

Bless you

This worked:

if (this.getField("KAQuestion7.3").isBoxChecked(0)==true && this.getField("KAQuestion7.1").isBoxChecked(0)==false && this.getField("KAQuestion7.2").isBoxChecked(0)==false && this.getField("KAQuestion7.4").isBoxChecked(0)==false) {

  this.getField("7Correct").display = display.visible

  this.getField("7Incorrect").display = display.hidden

  this.getField("CorrectAnswerTotal").value = Number(this.getField("CorrectAnswerTotal").value)+1;

} else {

  this.getField("7Incorrect").display = display.visible

  this.getField("7Correct").display = display.hidden

  this.getField("IncorrectAnswerTotal").value = Number(this.getField("IncorrectAnswerTotal").value)+1;

}

Thanks a billion more than the last thanks

Votes

Translate

Translate

Report

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