在javascript中使用多个嵌套的if语句

时间:2015-05-19 05:00:31

标签: javascript if-statement nested

我的理解是您可以堆叠多个嵌套的if语句,以便您可以验证表单上的用户输入。我试图组合两个嵌套的if语句,以便我可以检查多个字段的用户输入,例如他们提交时使用的运费和里程数,但它只通过第一个嵌套if语句的里程数?有没有更好的方法来编写脚本来完成这种任务?我一直在使用的代码如下。我的目标是通过消息提醒用户他们没有为他们的城外旅行输入任何里程信息和/或让他们提醒他们包括零件并询问他们是否命令他们将货运信息放入货运区域。如果两个问题都不存在,我希望它退出if语句并锁定表单减去客户的签名。

test = this.getField("City").value;
test1 = test.toUpperCase();

if (test1.indexOf("HOBBS") == -1) {
  if (this.getField("MileageTotal").value === 0) {
    app.alert("You have not listed any mileage for this trip.\n\n Please add mileage information for trips outside of the city.", 3, 0, "Trip Mileage Missing");
    app.alert(this.getField("MileageTotal").value);
  }
} else if (this.getField("TotalParts").value > 0 ){ 
  if(this.getField("Freight").value === 0) {
    result = app.alert("You have indicated that you have used parts on this job. \n Were any of these parts ordered?", 2, 2, "No Freight Entered");
  }
} else {
  for (var i = 0; i < this.numFields; i++){
    var fname = this.getNthFieldName(i);
    this.getField(fname).readonly = true;
  } // makes all fields readonly

  var f = this.getField("TechSigDate");
  f.value = util.printd("mmm/d/yyyy", new Date());
  this.getField("Customer Signature").readonly = false;
  this.getField("LockCustomer").readonly = false;
  this.getField("Lock Form").display = display.hidden;
} 

我感谢你们所有的建议。我试图结合这些陈述并使用&amp;&amp; amp;代替。这对里程很有用,但它会跳过货运验证并直接锁定表格。如果TotalParts大于0且Freight为0,我试图将其设置为true。我正在做&amp;&amp;陈述不正确?

test = this.getField("City").value;
test1 = test.toUpperCase();
if (test1.indexOf("HOBBS") == -1 && this.getField("MileageTotal").value === 0) {
  app.alert("You have not listed any mileage for this trip.\n\n Please add  mileage information for trips outside of the city.", 3, 0, "Trip Mileage Missing");
  app.alert(this.getField("MileageTotal").value);
}
if (this.getField("TotalParts").value > 0 &&this.getField("Freight").value === 0){
  result = app.alert("You have indicated that you have used parts on this job. \n Were any of these parts ordered?", 2, 2, "No Freight Entered");
} else {
  for (var i = 0; i < this.numFields; i++){
    var fname = this.getNthFieldName(i);
    this.getField(fname).readonly = true;
  } // makes all fields readonly

  var f = this.getField("TechSigDate");
  f.value = util.printd("mmm/d/yyyy", new Date());
  this.getField("Customer Signature").readonly = false;
  this.getField("LockCustomer").readonly = false;
  this.getField("Lock Form").display = display.hidden;
}

2 个答案:

答案 0 :(得分:0)

我会一直显示所有问题。

用户必须逐个修复它们,这很烦人。

test = this.getField("City").value;
test1 = test.toUpperCase();

alerts = []
if (test1.indexOf("HOBBS") === -1 && this.getField("MileageTotal").value === 0) {
    alerts.push("You have not listed any mileage for this trip.\n\n Please add mileage information for trips outside of the city.", 3, 0, "Trip Mileage Missing");
    alerts.push(this.getField("MileageTotal").value);
  }
}
if (this.getField("TotalParts").value > 0  && this.getField("Freight").value === 0) {
    alerts.push("You have indicated that you have used parts on this job. \n Were any of these parts ordered?", 2, 2, "No Freight Entered");
  }
}
if (alerts.length === 0) {
  this.numFields.forEach((files, index) => {
    var fname = this.getNthFieldName(index);
    this.getField(fname).readonly = true;
  }) // makes all fields readonly

  var f = this.getField("TechSigDate");
  f.value = util.printd("mmm/d/yyyy", new Date());
  this.getField("Customer Signature").readonly = false;
  this.getField("LockCustomer").readonly = false;
  this.getField("Lock Form").display = display.hidden;
} else {
  // Maybe you can also do alerts.forEach(app.alert)
  alerts.forEach(alert => app.alert)
}

答案 1 :(得分:-1)

如果要同时检查if语句,请删除'else'。这似乎不是排他性的情况,例如货运和里程是可能的,是吗?

相关问题