JS newbie here looking for assistance.
I have a form with multiple fields, in this instance Fields A, B and C
A second variable comes to play with fields A1, B1 and C1.
The second variables are determined from a drop down list:
var A1;
switch(this.getField("X").value) {
case 'SELECT VALUE' : X = ' '; break;
case 'ALPHA' : X = +1; break;
case 'BRAVO' : X = +2; break;
case 'CHARLIE' : X = +3; break;
case 'DELTA' : X = +4; break;
case 'ECHO' : X = +5;
}
event.value = X
var B1;
switch(this.getField("X").value) {
case 'SELECT VALUE' : X = ' '; break;
case 'ALPHA' : X = +0; break;
case 'BRAVO' : X = +0; break;
case 'CHARLIE' : X = +0; break;
case 'DELTA' : X = +0; break;
case 'ECHO' : X = +0;
}
event.value = X
var C1;
switch(this.getField("X").value) {
case 'SELECT VALUE' : X = ' '; break;
case 'ALPHA' : X = +5; break;
case 'BRAVO' : X = +4; break;
case 'CHARLIE' : X = +3; break;
case 'DELTA' : X = +2; break;
case 'ECHO' : X = +1;
}
event.value = X
I want to calculate the total of field A + field A1, etc.
var A = this.getField("A").value;
var A1 = this.getField("A1").value;
event.value = A + A1
var B = this.getField("B").value;
var B1 = this.getField("B1").value;
event.value = B + B1
var C = this.getField("C").value;
var C1 = this.getField("C1").value;
event.value = C + C1
At first the fields calculate properly.
Values inserted in fields A, B and C
A = 1
B = 2
C = 3
The drop down field "ALPHA" is selected
A1 = 1
B1 = 0
C1 = 5
fields A, B and C now calculate to:
A = 2
B = 2
C = 8
When I updated the value for instance field A from 1 to 3, fields B and C update and calculate again.
A = 4
B = 2
C = 13
How do i prevent the other fields from "auto" calculating and keep the values in the other fields static?
Where did you place the following code, exactly? Which field and which event?
var A = this.getField("A").value;
var A1 = this.getField("A1").value;
event.value = A + A1
If you could post a sample somewhere, it would be very helpful.
Also, code like the following:
case 'SELECT VALUE' : X = ' '; break;
will cause trouble since it's possible for the user to select this option, which will result in the value of the field getting set to a single space character. In a subsequent calculation that looks like you're wanting numeric addition, as opposed to string concatenation, this will cause trouble.
Finally, always make sure that the field calculation order makes sense for your form.