javascript - 用于表单的 onsubmit 函数即将出现未定义

标签 javascript html forms function onsubmit

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

enter image description here

这是必要的表单代码:

 <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;
//   }
}

脚本标签位于主体标签内,所以它应该可以工作..我哪里出错了?

最佳答案

请在表单中使用输入类型='提交'。它也可以在 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>

关于javascript - 用于表单的 onsubmit 函数即将出现未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49814445/

相关文章:

javascript - 使用 'zero' 数据破解 d3 饼图

html - 使用CSS隐藏rhandsontable中的列

forms - 我可以触发 Controller 提交的表单吗?

wpf - 如何使 Powershell WPF 表单在加载时运行一个函数(并显示自身)?

html - 将两个不同宽度的输入组并排对齐

javascript - 如何在现有的 iframe 上使用 YouTube API?

javascript - 通过R中的串扰使用选择框在R plotly图中选择默认值,使用静态html不 Shiny

javascript - 如何用D3做类似GitHub的时间线图?

javascript - 查看之前是否执行过onClick

javascript - 在 AngularJS 的 HTML img src 中使用字符串连接