我可以在JS中的else语句中插入if语句吗?

时间:2016-01-05 18:47:06

标签: javascript conditional

我可以在if语句中插入else语句吗? 代码:



function swicthNum() {
  'use strict';
  var processDoc = document.getElementById('process').innerHTML;
  if (processDoc == 'Dollars to Algerian Dinar') {
    processDoc = 'Algerian Dinar to Dollars';
  } else {
    processDoc = 'Dollars to Algerian Dinar';
  }
  return processDoc;
}
function moneyCalc() {
  'use strict';
  var num = document.getElementById('moneyNum').value,
      lastResultDoc = document.getElementById('lastResult'),
      result = num * 107.39,
      lastResult = document.getElementById('lastResult');
  if (num < 0) {
    lastResult.innerHTML = 'The process don\'t accept negative numbers';
  } else {
    if (processDoc == 'Dollars to Algerian Dinar') {
      lastResult.innerHTML = result;
    } else {
      result = num * 0.0093;
      lastResult.innerHTML = result;
    }
  }
}
&#13;
<p id="process">
  Dollars to Algerian Dinar
</p>
<button onclick="swicthNum()">Switch!</button>
<br />
<form action="AdvancedFunction.html" method="post">
  <input id="moneyNum" name="" type="number" value="" />
  <input onclick="moneyCalc()" name="" type="submit" value="Calculate" />
</form>
<div id="lastResult">Result</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

你绝对可以,但在你的情况下,你可以简单地去:

if (num < 0) {
  lastResult.innerHTML = 'The process don\'t accept negative numbers';
} 
else if (processDoc == 'Dollars to Euro') {
    lastResult.innerHTML = result;
} 
else {
    result = num * 0.0093;
    lastResult.innerHTML = result;
}

阅读可能更简单。

为什么你的代码不能正常工作

  • 您在函数中设置变量processDoc,因此它不适用于其他函数:you have to set it globally
  • “process”的innerHTML开头有空格,所以switchNum()从未改变过它的内容:using indexOf,我们可以检查段落中是否包含所需的字符串,但不一定相等到段落的内容。
  • 您没有在switchNum函数结束时更改进程的innerHTML。
  • 点击“计算”后,表单已提交,因此无法查看计算结果。为防止这种情况,请使用return false;

工作代码

function swicthNum() {
  'use strict';
  window.processDoc = document.getElementById('process').innerHTML;
  if (window.processDoc.indexOf('Dollars to Algerian Dinar')>-1) {
    window.processDoc = 'Algerian Dinar to Dollars';
  } else {
    window.processDoc = 'Dollars to Algerian Dinar';
  }
    document.getElementById('process').innerHTML = window.processDoc;
  return window.processDoc;
}
function moneyCalc() {
  'use strict';
  var num = document.getElementById('moneyNum').value,
      lastResultDoc = document.getElementById('lastResult'),
      result = num * 107.39,
      lastResult = document.getElementById('lastResult');
  if (num < 0) {
    lastResult.innerHTML = 'The process don\'t accept negative numbers';
  } else {
    if (window.processDoc == 'Dollars to Algerian Dinar') {
      lastResult.innerHTML = result;
    } else {
      result = num * 0.0093;
      lastResult.innerHTML = result;
    }
  }
}
<p id="process">Dollars to Algerian Dinar</p>
<button onclick="swicthNum()">Switch!</button>
<br />
<form action="#" method="post">
  <input id="moneyNum" name="" type="number" value="" />
  <input onclick="moneyCalc(); return false;" name="" type="submit" value="Calculate" />
</form>
<div id="lastResult">Result</div>