IF语句中的逻辑

时间:2014-10-08 22:06:52

标签: javascript onclick nested

我正在尝试构建一个具有以下逻辑的javascript按钮:

IF(Type = "A" AND (Field1 == "" OR Field2 == ""))
IF ELSE (Type = "B" AND (Field1 == "" OR Field2 == ""))
ELSE {script for approval}

我想弄清楚的是:

  1. 在最后只用一个ELSE语句连续编写IF语句
  2. 写入(Type =“A”AND(Field1 ==“”OR Field2 ==“”))的逻辑(嵌套OR语句,然后使用AND将其与第一个语句进行比较)
  3. 谢谢!

2 个答案:

答案 0 :(得分:1)

你需要更换你的if和's和Or来检查这个 也尝试用它来帮助自己测试...

http://jsfiddle.net/Egli/2a4apuwm/1/

//Set up your variables to test here
var Type = "A";
var Field1 = "";
var Field2 = "";

//Test conditions
if(Type == "A" && (Field1 == "" || Field2 == ""))
{
    alert("In: Condition 1");
}
else if(Type == "B" && (Field1 == "" || Field2 == ""))
{
    alert("In: Condition 2");
}
else
{
    alert("script for approval!");
}

哦,并确保你运行我发给你的小提琴(我发给你的链接)然后你可以用它来测试你的逻辑编辑等等:))

答案 1 :(得分:0)

if (any expression, the answer will be changed to a booleen true or false) {
  /* do something */
  if (expression B) {
     /* do something */
  } else {
     /* nested if else applies to expression B THE LAST IF STATEMENT* */
  }
} else if (Expression C) {
     /* do this is the main Expression was false but Expression C was true */
  } else {
     /* this block runs if Expression C was false, 
     which also means the main expression was false 
     Indentation for readability is a question of style. 
     Here it is indented to show ownership*/
}

你的逻辑没问题,你的语法很糟糕; Javascript区分大小写。并且声明,Type =" A"总是返回A ... MyType = Type =" A" / * MyType是" A"转换为true * /