【问题标题】:"If" "else" statement with syntax error [closed]带有语法错误的“If”“else”语句[关闭]
【发布时间】:2013-05-13 20:45:11
【问题描述】:

在第 11、12、13 行出现以下代码语法错误问题。

// Get the field values
var DP = +getField("DESIGN_Projection").value;
var TC = +getField("ASBUILT_Top_of_Concrete").value;
var GE = +getField("ASBUILT_Ground_Elevation").value;



// If DP is N/A, set this field to display N/A
If (DP === N/A); {
    event.value = "NA";  // display N/A in this field
} else 
{
    //...otherwise, set this field value to the result of the following calculation
    event.value = ((TC - GE) * 1000);    
}

【问题讨论】:

  • 这不是java吗?对我来说它看起来像 javascript。

标签: javascript if-statement syntax


【解决方案1】:

这行有几个问题:

If (DP === N/A); {

首先,请注意If 应为小写(if)。其次,注意if 语句后面有一个分号。这使得语言认为你的代码应该被解释为

If (DP === N/A)
   ;  // Do nothing

{
    event.value = "NA";  // display N/A in this field
} else 
{
    //...otherwise, set this field value to the result of the following calculation
    event.value = ((TC - GE) * 1000);    
}

从这里,应该更清楚错误是什么了——有一个神秘的else漂浮在周围!

如果您删除分号并将If 更改为if,错误应该会消失。

希望这会有所帮助!

【讨论】:

  • if 语句的基本设置就是 "if () { something; }
【解决方案2】:

If 应为 if(小写 i)

同时从 if 中删除 ;

if (DP === N/A) {
    event.value = "NA";  // display N/A in this field
} else 

【讨论】:

    【解决方案3】:

    if 语句末尾有一个分号。

    if也应该小写

    【讨论】: