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

这个问题在这里已经有了答案:

  • 你如何检查JavaScript中的空字符串? 39个答案

  • 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>

    你在找什么?

    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>

    链接地址: http://www.djcxy.com/p/27515.html

    上一篇: How can I check if the input is empty in JavaScript?

    下一篇: Javascript: How to check if a string is empty?