Salir
  • Comunidad global
    • Idioma:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티

Javascript calculation after values entered.

Principiante de comunidad ,
Aug 23, 2017 Aug 23, 2017

I currently have the following java script in a adobe pdf to perform calculations.  One of the problems I am running into is the form is performing the calculations live.  I do not want the calculations to be performed till values are entered.  So currently the 3 text boxes are "Neck1", "Neck2" and "Neck3".  Once values are entered into these boxes I would like to perform the average calculation.  Any help would be appreciated.  I am not experience with JavaScript so please be very detailed with answers.

// Custom calculation script (function () {

// Get the field values, as numbers

var v1 = +getField("Neck1").value;

var v2 = +getField("Neck2").value;

var v3 = +getField ("Neck3").value;

var nTotal = (v1 + v2 + v3)

var nAverage = (nTotal / 3)

var nRounded = Math.round(nAverage / .5)*.5;

event.value = nRounded; })();

TEMAS
Acrobat SDK y JavaScript
1.4K
Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines

correct answers 1 respuesta correcta

Community Expert , Aug 23, 2017 Aug 23, 2017

Use this code:

(function () {

    var s1 = getField("Neck1").valueAsString;

    var s2 = getField("Neck2").valueAsString;

    var s3 = getField("Neck3").valueAsString;

    if (s1!="" && s2!="" && s3!="") {

        var v1 = +s1;

        var v2 = +s2;

        var v3 = +s3;

        var nTotal = (v1 + v2 + v3)

        var nAverage = (nTotal / 3)

        var nRounded = Math.round(nAverage / .5)*.5;

        event.value = nRounded;

    } else event.value = "";

      

})();

Traducir
Community Expert ,
Aug 23, 2017 Aug 23, 2017

Do you mean that you don't want the calculation to occur until all three fields are non-empty?

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Principiante de comunidad ,
Aug 23, 2017 Aug 23, 2017

Yes.  That is correct.  I do not want the calculation to occur until all three fields are non-empty.

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Community Expert ,
Aug 23, 2017 Aug 23, 2017

Use this code:

(function () {

    var s1 = getField("Neck1").valueAsString;

    var s2 = getField("Neck2").valueAsString;

    var s3 = getField("Neck3").valueAsString;

    if (s1!="" && s2!="" && s3!="") {

        var v1 = +s1;

        var v2 = +s2;

        var v3 = +s3;

        var nTotal = (v1 + v2 + v3)

        var nAverage = (nTotal / 3)

        var nRounded = Math.round(nAverage / .5)*.5;

        event.value = nRounded;

    } else event.value = "";

      

})();

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Principiante de comunidad ,
Aug 23, 2017 Aug 23, 2017

That worked for a portion of the calculation.  I see in the code you sent me that

var s1 = getField("Neck1").valueAsString

is how to pull a value from a Field.  How do you move a value to a "Field"?

I currently have a bunch of small fields that contain calculations and these values field more calculations.  It seems to not be executing properly due to this.  I am wanting to form just one hug calculation and once the values are found then push the answers to multiple fields.

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines
Community Expert ,
Aug 23, 2017 Aug 23, 2017
MÁS RECIENTES

To apply a value of a field (not from its own calculation script), use this:

this.getField("FieldName").value = "New field value";

Traducir
Informe
Directrices de la comunidad
Sé amable y respetuoso, muestra títulos de crédito de la fuente de contenido original y busca duplicados antes de publicar. Más información
community guidelines