如何检查 JavaScript 中的输入是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30624789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:29:04  来源:igfitidea点击:

How can I check if the input is empty in JavaScript?

javascript

提问by Sajib Devnath Akash

I am trying to make a factorial calculator. How can I check if the input is empty or not? I tried 'null'. But it didn't work or I couldn't use it properly.
sorry for the stupid question. I am newbie in JavaScript

我正在尝试制作阶乘计算器。如何检查输入是否为空?我试过'空'。但它不起作用或我无法正确使用它。
抱歉这个愚蠢的问题。我是JavaScript新手

function myFriday() {
  var input = document.getElementById("input1").value;

  var ever = function () {
    if( !(isNaN(input))) {
      var result = 1;
      for(var i = 1; i <= input; i++ ) {
        result = result * i
      }
      return result;
    }
    else if (input == null){
      return "Please input a number"
    }
    else{
      return "Please input a number"
    }
  }

  document.getElementById("input2").value = ever();
}
<p>Input: <input type="text" id = "input1" /></p>
<p>Input: <input type="text" id = "input2" /></p> 
<button onclick = "myFriday()">Calculate</button>
<p >RESULT: <span id = "result" style = "color:red"></span> </p>

回答by AmmarCSE

function myFriday() {
  var input = document.getElementById("input1").value;

  var ever = function() {
    if (input.trim() == '') {
      return "Please input a number"
    } else if (!(isNaN(input))) {
      var result = 1;
      for (var i = 1; i <= input; i++) {
        result = result * i
      }
      return result;
    }

  }

  document.getElementById("input2").value = ever();
}
<p>Input:
  <input type="text" id="input1" />
</p>
<p>Input:
  <input type="text" id="input2" />
</p>
<button onclick="myFriday()">Calculate</button>
<p>RESULT: <span id="result" style="color:red"></span> 
</p>

回答by NazKazi

is that what you looking for?

这就是你要找的吗?

function myFriday() {
  var input = document.getElementById("input1").value;

  var ever = function () {
    if(input.match(/\D/) == null){ // changes made here
      var result = 1;
      for(var i = 1; i <= input; i++ ) {
        result = result * i
      }
      return result;
    }
    else{ // one else is enough
      return "Please input a number"
    }
  }

  document.getElementById("input2").value = ever();
}
<p>Input: <input type="text" id = "input1" /></p>
<p>Input: <input type="text" id = "input2" /></p> 
<button onclick = "myFriday()">Calculate</button>
<p >RESULT: <span id = "result" style = "color:red"></span> </p>

回答by Jeroen Vervaeke

When you use .value you get a string value in return. This means that when you enter nothing in the input it'll return ""

当您使用 .value 时,您会得到一个字符串值作为回报。这意味着当您在输入中不输入任何内容时,它将返回“”

So you should change this piece of code input == null

所以你应该改变这段代码 input == null

Into this input === ""

成这个 input === ""

Note that I also wrote === instead of == Using === in javascript is faster than == when the objects are of the same type.

请注意,当对象是相同类型时,我还写了 === 而不是 == 在 javascript 中使用 === 比 == 快。