【问题标题】:onsubmit function used for form is coming up undefined用于表单的 onsubmit 函数即将出现未定义
【发布时间】:2018-04-13 10:06:38
【问题描述】:

我想验证我的表单,并使用 onsubmit 创建了一个 formValidation 函数,该函数从 js.js 文件中调用一个函数。但是,当我单击提交时,我收到此错误:

这是必要的表单代码:

 <form action="send-email.php" id="callback" class="popup-form" method="post" onsubmit="return validateForm()">
 // Form content
</form>

这里是功能代码:

function validateForm() {
  console.log("hellloooooo this is working");
//   var captcha_response = grecaptcha.getResponse();
//   if(captcha_response.length === 0)
//   {
//       // Captcha is not Passed
//       console.log("not working");
//       return false;
//   }
//   else
//   {
//       // Captcha is Passed
//       console.log("working");
//       return true;
//   }
}

脚本标签在正文标签内,所以它应该可以工作..我哪里出错了??

【问题讨论】:

  • 请发布您的完整 HTML 代码。
  • 在线处理程序只能找到全局函数。
  • @Teemu 对,我的错……已删除。

标签: javascript html forms function onsubmit


【解决方案1】:

请在表单中使用 input type='submit'。它也可以在 java 脚本中运行。

在下面的表单中添加提交输入

<form action="send-email.php" id="callback" class="popup-form" method="post" onsubmit="return validateForm()"><input type="submit">
</form>

并在 java 脚本中编写您的验证函数,如下所示

<script type="text/javascript">
  function validateForm(){
    console.log('ddd');
    return false;
  }
</script>

【讨论】:

    【解决方案2】:

    你应该确保你的脚本标签在你的表单标签之上

    【讨论】:

      【解决方案3】:

      这似乎有效。在这里看不到任何问题

      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>Page Title</title>
      
      </head>
      
      <body>
          <script>
              function validateForm() {
                  console.log("hellloooooo this is working");
              }
          </script>
          <form id="callback" class="popup-form" method="post" onsubmit="return validateForm()">
              // Form content
              <input type="Submit">
          </form>
      
      </body>
      
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多