Answered
I am having issues with getting a simple addition calculation to work correctly. Ok, this will take a bit to explain the details so bear with me.
I have a text field (ST.0) which is calculating the total of 5 other text fields (SL, SMo.0, SR.0, SMi.0 and SA) as well as adding 5 to the total if a check box (T.0) is checked. Here is the code for ST.0:
var f = this.getField("T.0"); var g = this.getField("SL"); var h = this.getField("SMo.0"); var i = this.getField("SR.0"); var j = this.getField("SMi.0"); if (f.isBoxChecked(0)) { event.value = 5 + g.value + h.value + i.value + j.value + this.getField("SA").value } else { event.value = g.value + h.value + i.value + j.value + this.getField("SA").value }
Fields SL, SMo.0 and SA are automatically filled from other fields in the document, while SR.0 and SMi.0 are filled by user input.
Here is the problem:
If SL = x, SMo.0 = y and SA = z (where x,y and z are numbers), and SR.0 and SMi.0 have nothing in them (not even a 0), then the output for ST.0 is (1+x+y)0z, ie if x=1, y=2 and z=3 then ST.0 is 33. However, if SR.0 and SMi.0 have a 0 in them, the calculation is correct. Is there any way to have this calculate without having to enter 0 into SR.0 and SMi.0?
So much for a "complicated" explanation.
What it means to general use, is to force the values to be of type "number". And the easiest way to do it is multiplying it with 1... so, instead of
foo.value
you would write
foo.value*1
And that will do it.
Hope this can help.
Max Wyss.