If 语句和条件

If statements and conditions

我正在尝试在警告框中确认两个不同的事情,if 语句。第一个是用户按下是按钮,第二个是页面上的用户输入值。请参阅下面的代码。我还是很新手,如果有任何帮助,我们将不胜感激。

var cMsg = "You are about to reset this page!";
cMsg += "\n\nDo you want to continue?";

var nRtn = app.alert(cMsg,2,2,"Question Alert Box");
if(nRtn == 4) && (getField("MV").value == 5)
{
////// do this

}
else if(nRtn == 4) && (getField("MV").value == 6)
{
/////then do this
}
else if(nRtn == 4) && (getField("MV").value == 7)
{
/////then do this
}

}
else if(nRtn == 3)
{
console.println("Abort the submit operation");
}
else
{ //Unknown Response
console.println("The Response Was somthing other than Yes/No: " + nRtn);
}

您的 if...else 语法不正确。正确语法

if (condition)
   statement1
[else
   statement2]

使用正确的语法

if (nRtn == 4 && getField("MV").value == 5) {
    ////// do this    
} else if (nRtn == 4 && getField("MV").value == 6) {
    /////then do this
} else if (nRtn == 4 && getField("MV").value == 7) {
    /////then do this
} else if (nRtn == 3) {
    console.println("Abort the submit operation");
} else { //Unknown Response
    console.println("The Response Was somthing other than Yes/No: " + nRtn);
}

而不是

if (nRtn == 4) && (getField("MV").value == 5) {
    ////// do this

} else if (nRtn == 4) && (getField("MV").value == 6) {
    /////then do this
} else if (nRtn == 4) && (getField("MV").value == 7) {
    /////then do this
} <=== Remove this 

} else if (nRtn == 3) {
    console.println("Abort the submit operation");
} else { //Unknown Response
    console.println("The Response Was somthing other than Yes/No: " + nRtn);
}

您正在尝试评估两个不同的条件: "nRtn" // 从 app.alert(cMsg,2,2,"Question Alert Box") 返回的值; 和:getField("MV").value.

但是,编译器只会处理第一个条件,因为大括号在那里结束。您应该确保将所有条件包含在一个大括号中。个别条件可以并且按照惯例也应该在主括号内的单独括号中。 因此正确的方法应该是:

if ((nRtn == 4) && (getField("MV").value == 5))
//notice the initial if (( and terminating ))
//You could have 3 conditions as follows
// if (((condition1) && (condition2)) && (condition3)))
{
////// do this

}
else if((nRtn == 4) && (getField("MV").value == 6))
{
/////then do this
}
else if((nRtn == 4) && (getField("MV").value == 7))
{
/////then do this
}

}
else if(nRtn == 3)
{
console.println("Abort the submit operation");
}
else
{ //Unknown Response
console.println("The Response Was somthing other than Yes/No: " + nRtn);
}