基于javascript中另一个字段中的条件的sum字段值

时间:2018-06-15 16:19:06

标签: javascript foxit

我正在尝试对多个字段值求和,但前提是满足与这些字段值相关联的条件。例如:

我有一个包含多个数字值字段的表单。每个号码字段都附有状态选择。我只想添加相同状态的所有数字字段,或者可能添加一些具有不同状态的字段。这是我尝试的代码,但不起作用:

// field names
var cTotal1 = "Day1Pd1Total";
var cTotal2 = "Day1Pd2Total";
var cTotal3 = "Day1Pd3Total";
var cTotal4 = "Day1Pd4Total";
var cTotal5 = "Day1Pd5Total";
var cTotal6 = "Day1Pd6Total";
var cStatus1 = "Day1Pd1Status";
var cStatus2 = "Day1Pd2Status";
var cStatus3 = "Day1Pd3Status";
var cStatus4 = "Day1Pd4Status";
var cStatus5 = "Day1Pd5Status";
var cStatus6 = "Day1Pd6Status";
// get conditonal field values
var vStatus1 = this.getField(cStatus1).value;
var vStatus2 = this.getField(cStatus2).value;
var vStatus3 = this.getField(cStatus3).value;
var vStatus4 = this.getField(cStatus4).value;
var vStatus5 = this.getField(cStatus5).value;
var vStatus6 = this.getField(cStatus6).value;
// get field values base on condition
if(vStatus1 = "1"){
var vTotal1 = this.getField(cTotal1).value;
}else{
var vTotal1 = "0"; 
}
if(vStatus2 = "1"){
var vTotal2 = this.getField(cTotal2).value;
}else{
var vTotal2 = "0";
}
if(vStatus3 = "1"){
var vTotal3 = this.getField(cTotal3).value;
}else{
var vTotal3 = "0"; 
}
if(vStatus4 = "1"){
var vTotal4 = this.getField(cTotal4).value;
}else{
vTotal4 = "0";
}
if(vStatus5 = "1"){
var vTotal5 = this.getField(cTotal5).value;
}else{
vTotal5 = "0";
}
if(vStatus6 = "1"){
var vTotal6 = this.getField(cTotal6).value;
}else{
vTotal6 = "0";
}
// clear result value
event.value = "";
// add values
var FLPTotal = vTotal1+ vTotal2 + vTotal3 + vTotal4 + vTotal5 + vTotal6;
event.value = FLPTotal;

我也尝试过这样的事情:

var FLPTotal = vTotal1(vStatus1 == "1") + vTotal2(vStatus2 == "1") +vTotal3(vStatus3 == "1") +vTotal4(vStatus4 == "1") +vTotal5(vStatus5 == "1") +vTotal6(vStatus6 == "1")

我承认,我对JavaScript的了解相当糟糕。任何帮助都将得到真诚的赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

在进行了更多工作之后,我注意到它充满了语法错误。修好之后,我就可以开始工作了:

// field names
var cTotal1 = "Day1Pd1Total";
var cTotal2 = "Day1Pd2Total";
var cTotal3 = "Day1Pd3Total";
var cTotal4 = "Day1Pd4Total";
var cTotal5 = "Day1Pd5Total";
var cTotal6 = "Day1Pd6Total";
var cStatus1 = "Day1Pd1Status";
var cStatus2 = "Day1Pd2Status";
var cStatus3 = "Day1Pd3Status";
var cStatus4 = "Day1Pd4Status";
var cStatus5 = "Day1Pd5Status";
var cStatus6 = "Day1Pd6Status";
// get conditonal field values
var vStatus1 = this.getField(cStatus1).value;
var vStatus2 = this.getField(cStatus2).value;
var vStatus3 = this.getField(cStatus3).value;
var vStatus4 = this.getField(cStatus4).value;
var vStatus5 = this.getField(cStatus5).value;
var vStatus6 = this.getField(cStatus6).value;
// get field values base on condition
if(vStatus1 == "1"){
var vTotal1 = this.getField(cTotal1).value;
}else{
var vTotal1 = "";
}
if(vStatus2 == "1"){
var vTotal2 = this.getField(cTotal2).value;
}else{
var vTotal2 = "";
}
if(vStatus3 == "1"){
var vTotal3 = this.getField(cTotal3).value;
}else{
var vTotal3 = "";
}
if(vStatus4 == "1"){
var vTotal4 = this.getField(cTotal4).value;
}else{
var vTotal4 = "";
}
if(vStatus5 == "1"){
var vTotal5 = this.getField(cTotal5).value;
}else{
var vTotal5 = "";
}
if(vStatus6 == "1"){
var vTotal6 = this.getField(cTotal6).value;
}else{
var vTotal6 = "";
}
// clear result value
event.value = "";
// add values
var FLPTotal = vTotal1 + vTotal2 + vTotal3 + vTotal4 + vTotal5 + vTotal6;
event.value = FLPTotal;

我不确定它是否尽可能精简,但它正在做我需要的事情。