【问题标题】:Javascript Data Type ConditionJavascript 数据类型条件
【发布时间】:2019-01-11 18:01:45
【问题描述】:

今天我发现了一个状态不佳的巨魔。但基本是 “如果我通过了考试,我必须安排一个聚会。但如果我考试不及格,我必须认真学习”

这是一个简单的 if, else 条件。但是我遇到了太多的问题。我不得不提到我正在学习JS。这么简单的问题对我来说似乎很难。

我为“考试通过或失败”设置了 if、else if、else 条件。但是当我输入一个值 excpet“通过条件”时,我感到震惊。

注意:我将“通过”标记为“好”,将“失败”标记为“坏”。

我所面临的让我解释一下:(

  1. 当我输入“好”字之外的时候。然后我需要一条消息来输入“好”或“坏”

  2. 如果我输入任何数字,那么我需要一条消息来输入“好”或“坏”

  3. 如果我提交的输入字段没有任何值。然后我需要一条消息来输入“好”或“坏”或其他内容

我自己编写了一个脚本。但是我遇到了太多没有答案的问题。

  1. 如果我按照此模式“textnumber”输入输入 例如。 “好123” 然后它显示未定义。我不知道为什么:( 为什么将“good123”标记为“typeof”“未定义”????

  2. 对我来说最神秘的事情。如果我输入“好”,则显示正确的条件输出,但如果我输入“好”,则显示未定义。 我的意思是:如果我输入区分大小写的单词,那么输入不满足条件。在我的示例中,我以某种方式解决了它。但是请找我,有什么办法可以解决这个问题。

最后,我以自己的方式制作了一个脚本。可能有任何其他方式来创建此项目,或者可以以替代方式简化脚本。如果您建议我这样做,我将不胜感激。还请建议我如何克服我面临的问题。没有我的方式来解决这些指出的问题的任何替代方法。

问候 Md Rizaur

<!--The Script Is Made By Md Rizaur Rahman from https://stackoverflow.com/users/9192572/md-rizaur-rahman-->
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Simple Logic By Md Rizaur Rahman</title>
</head>
<body>
<script>
function logic() {
    var userInput = document.getElementById("logic").value;
    var input = userInput.toLowerCase();
    var onlyStr = /^[a-zA-z]+$/;
    var onlyNum =/^[0-9]+$/;
    var output;
    if(input === "good"){
        output = "Lets Party"
    }
    else if(input === "bad"){
        output = "Please Study Attentively"
    }
    else if ((input.match(onlyStr)) && (input !== "good")){
        output = "You Enter String Without \"Good\" Word. Please Enter Good or Bad";
    }
    else if(input.match(onlyNum)){
        output = "You Enter A Number. Please Enter Good or Bad";
    }
    else if (input.trim() == ""){
        output = "Please Enter Any Value or Please Enter Good or Bad";
    }
    else{
        output = "Your Input Type Is Marked As Undefined. Please Enter Any Value or Please Enter Good or Bad";
    }
    alert (output);
}


</script>

<input type="text" id="logic">
<button onclick="logic()" type="submit" id="input">Submit</button>
<p id="html_output"></p>

</body>
</html>

【问题讨论】:

  • 不要使用正则表达式来检查类型,而是使用typeof
  • @Pavitra typeof 总是会返回 string
  • 1) ""good123" 与任一正则表达式都不匹配。(它不是“未定义”。它只是不匹配任何条件)和 2) 在此页面上工作正常。
  • @Keith 在他的代码中,他尝试在第 3 和第 4 个条件下使用正则表达式检查输入是字符串还是数字。
  • @JohnnyMopp:用户可以在输入字段中输入任何大写或小写字符串。所以我必须使用“var input = userInput.toLowerCase();”创建一个变量,强制字符串变为小写。方法不对吗?

标签: javascript typeof


【解决方案1】:

它被标记为未定义,因为它不满足上述任何条件(它不匹配正则表达式并且不是“好”或“坏”)。将您的正则表达式更改为 /^[a-zA-z0-9]+$/(接受 0-9 的数字)并使用 !isNaN(input)else if 前面检查数值。在检查它是否等于“好”或“坏”之前,您还应该将输入更改为小写。要使isNaN 检查正常工作,您还需要检查input 是否为所有空格(if(!input.trim().length)),因为空格也被视为数字(不是 NaN)。

<!--The Script Is Made By Md Rizaur Rahman from https://stackoverflow.com/users/9192572/md-rizaur-rahman-->
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Simple Logic By Md Rizaur Rahman</title>
</head>
<body>
<script>
function logic() {
    var userInput = document.getElementById("logic").value;
    var input = userInput.toLowerCase();
    var onlyStr = /^[a-zA-z0-9]+$/;
    var onlyNum =/^[0-9]+$/;
    var output;
    if(input.toLowerCase() === "good"){
        output = "Lets Party"
    }
    else if(input.toLowerCase() === "bad"){
        output = "Please Study Attentively"
    } 
    else if(!input.trim().length){
      output = "Input can not be empty!";
    }
    else if(!isNaN(input)){
     output = "You Enter A Number. Please Enter Good or Bad";
    }
    else if ((input.match(onlyStr)) && (input !== "good")){
        output = "You Enter String Without \"Good\" Word. Please Enter Good or Bad";
    }
    else if (input.trim() == ""){
        output = "Please Enter Any Value or Please Enter Good or Bad";
    }
    else{
        output = "Your Input Type Is Marked As Undefined. Please Enter Any Value or Please Enter Good or Bad";
    }
    alert (output);
}


</script>

<input type="text" id="logic">
<button onclick="logic()" type="submit" id="input">Submit</button>
<p id="html_output"></p>

</body>
</html>

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多