如果和其他语句总是输出如果

时间:2017-04-26 02:46:37

标签: javascript html

我正在搞乱以前存在的片段并遇到了一个问题。 每当我尝试输入一个不会应用于If语句的输入时,它总是给我If输出。此外,我一直在寻找,而不是说已批准,您是否已将您的网址发送到Google,例如Google,而且我不确定这两者中的任何一个的解决方案。



function myFunction() {
  var a = document.getElementById("text_a").value;

  if (a == "02035", "02048", "02067") {
    document.getElementById("answer").innerHTML = "Approved";
  } else {
    document.getElementById("answer").innerHTML = "Our service currently isn't available in your area! Try again soon!";
  }
}

<p>Enter Zip Code </p>
<input id="text_a" type="text" />
<p id="answer"></p>
<button onclick="myFunction()">Check</button>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您可能需要更改if声明中的条件:

if (a == "02035" || a == "02048" || a == "02067"){
  document.getElementById("answer").innerHTML="Approved";
}
else{
  document.getElementById("answer").innerHTML="Our service currently isn't available in your area! Try again soon!";
}

答案 1 :(得分:1)

if (a=="02035","02048","02067")

你不能这样做。 有很多方法可以检查它。你可以这样做

function myFunction() {
  var a = document.getElementById("text_a").value;
  var list = ["02035", "02048", "02067"];
  console.log(a)
  if ((list.indexOf(a) > -1)) {
    document.getElementById("answer").innerHTML = "Approved";
  } else {
    document.getElementById("answer").innerHTML =
      "Our service currently isn't available in your area! Try again soon!";
  }
}

我做了一个样本:https://codepen.io/anon/pen/bWBvMj?editors=1011