【问题标题】:if Function is returning the same output如果函数返回相同的输出
【发布时间】:2013-03-01 22:40:33
【问题描述】:

当用户选择一个答案时,下面的代码应该给出不同的结果;但是,它只是给出相同的结果。为什么按下按钮时会给出相同的结果?

1.Is the network problem affecting just your pc? <BR>

<select id="Area">

  <option value="Yes">Yes</option>

  <option value="No">No</option>

  </select>

<br>

<br><p>



2.Can you access the internet? <BR> 



 <select id="Internet">

  <option value="Yes">Yes</option>

  <option value="No">No</option>

  <option value="Yes,but is slow">Yes,but is slow</option>

  <option value="Drop in connection">Drop in connection</option>

</select>

<br>

<br><p>



3.Are you receiving any error message saying remote system could not be found or connection has timed out?<BR>

<select id="Errors">

  <option value="Remote">Remote system could not be found</option>

  <option value="Connection">Connection has timed out</option>

  <option value="No">No error messages </option>

  </select>

<br>

<br><p>



4.Have you changed any software or Hardware before the problem occurred?<BR> 



 <select id="Change">

  <option value="Software">Software</option>

  <option value="Hardware">Hardware</option>

  <option value="Both">Both</option>

  <option value="None">None </option>

</select>

<br>

<br><p>





<button onclick="myFunction()">Detect</button>



<p id="Solution"></p>



<script>

function myFunction()

{

    var AreaElem =  document.getElementById("Area");

    var Area = AreaElem.options[AreaElem.selectedIndex].value;

    var InternetElem =  document.getElementById("Internet");

    var Internet= InternetElem.options[InternetElem.selectedIndex].value;
    var ErrorsElem =  document.getElementById("Errors");

    var Errors= ErrorsElem.options[ErrorsElem.selectedIndex].value;
    var ChangeElem =  document.getElementById("Change");

    var Change= ChangeElem.options[ChangeElem.selectedIndex].value;



   if (Area=="Yes"||Internet=="Yes"||Errors=="No"||Change=="No") {
      x="check your cable is not cut or loose";
} else if (Area=="Yes"||Internet=="Yes"||Errors=="No"||Change=="Both") {
      x="network connections ";
   } else {
      x="other problems....";
   }
   document.getElementById("Solution").innerHTML=x;
}

</script>

【问题讨论】:

  • 我可以理解这样的编码,如果它是一个小的关闭。但是,如果您有很多这样的问答形式,我认为您应该使用更强大的方式来完成所有这些工作,例如将所有约束放在服务器上并使用 ajax 返回结果。

标签: javascript html logic


【解决方案1】:
   if (Area=="Yes"||Internet=="Yes"||Errors=="No"||Change=="No") {
      x="check your cable is not cut or loose";
} else if (Area=="Yes"||Internet=="Yes"||Errors=="No"||Change=="Both") {
      x="network connections ";
   } else {
      x="other problems....";
   }

问题就在这里!您正在使用“或”运算符|| 您应该使用“和”运算符&amp;&amp; 这应该可以解决它

   if (Area=="Yes"&&Internet=="Yes"&&Errors=="No"&&Change=="No") {
      x="check your cable is not cut or loose";
} else if (Area=="Yes"&&Internet=="Yes"&&Errors=="No"&&Change=="Both") {
      x="network connections ";
   } else {
      x="other problems....";
   }

【讨论】:

  • 是的,这就是问题所在。太感谢了! :)
  • 好答案。只是一个附录:当你使用“或”时,你是在说,“如果这些选项中的 any 为真,请执行以下操作......”,但是当你使用“and”时,你是说,“如果所有这些选项都为真,请执行以下操作...”
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多