提交按钮不适用于使用JavaScript验证表单

时间:2017-10-31 20:45:09

标签: javascript jquery html

当我点击提交按钮时,我编写了一个代码。文本只显示不到1秒钟,我该如何永久制作?



<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Untitled Document</title>
</head>

<body>
  <form action="#" method="post" onSubmit="return validcheck()">
    <input type="text" id="firstname" name="firstname">
    <input type="submit">
    <div id="error1"></div>
  </form>

  <script>
    function validcheck() {
      var checkingg = document.getElementById('firstname').value;
      if (checkingg == null || checkingg == "") {
        document.getElementById('error1').innerHTML = 'please write something here';
      } else {
        document.getElementById('name-error').innerHTML = '';
      }
    }
  </script>
</body>

</html>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

从你的方法中返回false:

    function validcheck() {
      var checkingg = document.getElementById('firstname').value;
      if (checkingg == null || checkingg == "") {
        document.getElementById('error1').innerHTML = 'please write something here';
      } else {
        document.getElementById('name-error').innerHTML = '';
      }
      return false;
    }

答案 1 :(得分:0)

您缺少退货声明

<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Untitled Document</title>
</head>

<body>
  <form action="#" method="post" onSubmit="return validcheck()">
    <input type="text" id="firstname" name="firstname">
    <input type="submit">
    <div id="error1"></div>
  </form>

  <script>
    function validcheck() {
      var checkingg = document.getElementById('firstname').value;
      if (checkingg == null || checkingg == "") {
        document.getElementById('error1').innerHTML = 'please write something here';
      } else {
        document.getElementById('name-error').innerHTML = '';
      }
 return false;
    }
  </script>
</body>

</html>

答案 2 :(得分:0)

您可以采取以下措施。

 <form action="javascript:void(0)" method="post" onSubmit="return validcheck()">