if子句在JavaScript中指定工作流?

if子句在JavaScript中指定工作流?,javascript,html,Javascript,Html,当我看下面的代码时,我认为“CheckChoice(IntSelect)”将始终执行,而不管“if(isNaN(IntSelect))”是否执行 如果我输入了一个NaN,并且CheckChoice函数起作用,警报(“提供的值超出范围!”);还应显示为“默认”分支。但这并没有像我预期的那样发生 我不明白当输入为NaN时会发生什么,有人能解释一下吗 多谢各位 <!DOCTYPE html> <html> <head> <title>Using

当我看下面的代码时,我认为“CheckChoice(IntSelect)”将始终执行,而不管“if(isNaN(IntSelect))”是否执行

如果我输入了一个NaN,并且CheckChoice函数起作用,警报(“提供的值超出范围!”);还应显示为“默认”分支。但这并没有像我预期的那样发生

我不明白当输入为NaN时会发生什么,有人能解释一下吗

多谢各位

<!DOCTYPE html>

<html>
<head>
   <title>Using the Default Clause</title>
   <script language="JavaScript">
      function CheckChoice(option)
      {
         // Make a selection.
         switch (option)
         {
            case 1:
               document.getElementById("Result").innerHTML =
                  "You chose Item A.";
               break;
            case 2:
               document.getElementById("Result").innerHTML =
                  "You chose Item B.";
               break;
            case 3:
               document.getElementById("Result").innerHTML =
                  "You chose Item C.";
               break;
            default:
               // Display an error dialog.
               alert("The value supplied is out of range!");
               break;
         }
      }

      function MakeAChoice()
      {
         // Ask the user to provide input.
         var Selection = prompt("Type a menu option.");

         // Convert the string to a number.
         var IntSelect = parseInt(Selection);

         // Verify the user has provided a number.
         if (isNaN(IntSelect))
         {
            // Display an error dialog.
            alert("Please provide numeric input!");

            // Return without doing anything more.
            return;
         }

         // Call the selection function.
         CheckChoice(IntSelect);
      }
   </script>
</head>

<body>
   <h1>Using the Default Clause</h1>
   <p>Menu Options:</p>
   <ol>
    <li>Item A</li>
    <li>Item B</li>
    <li>Item C</li>
   </ol>
   <input type="button"
          value="Choose a Menu Item"
          onclick="MakeAChoice()" />
   <p id="Result"></p>
</body>
</html>

使用Default子句
功能检查选项(选项)
{
//做出选择。
开关(选件)
{
案例1:
document.getElementById(“结果”).innerHTML=
“您选择了项目A.”;
打破
案例2:
document.getElementById(“结果”).innerHTML=
“您选择了项目B.”;
打破
案例3:
document.getElementById(“结果”).innerHTML=
“您选择了C项”;
打破
违约:
//显示一个错误对话框。
警报(“提供的值超出范围!”);
打破
}
}
函数MakeAChoice()
{
//请用户提供输入。
变量选择=提示(“键入菜单选项”);
//将字符串转换为数字。
var IntSelect=parseInt(选择);
//验证用户是否提供了号码。
if(isNaN(IntSelect))
{
//显示一个错误对话框。
警报(“请提供数字输入!”);
//什么也不做就回来。
返回;
}
//调用选择函数。
选中选择(IntSelect);
}
使用Default子句
菜单选项:

  • 项目A
  • B项
  • 项目C

  • javascript中的Return结束函数。如果IntSelect为NaN,则输入一个带返回的代码块。然后在执行CheckCoice()之前返回函数。

    if
    块中有一个
    return
    语句。你知道这是什么吗?那上面那句话的注释呢?如果
    isNaN
    运行,那么
    CheckChoice
    就会变成不可访问的代码。我不知道返回结束了JS中的一个函数。你的答案很全面。非常感谢。