简体   繁体   English

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

[英]submit button is not working for validating form with JavaScript

I wrote a code when I click on the submit button The text will appear just for less than 1 second how can I make it forever? 当我单击“提交”按钮时,我写了一个代码。文本将仅显示不到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> 

Just return false from your method: 只需从您的方法返回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;
    }

You are missing return statement 您缺少退货声明

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

您可以采取以下措施。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM