首页 > 解决方案 > 表单验证功能在提交时不运行

问题描述

我有一个用于验证表单值的函数。我已将它添加到onsubmit属性中,但它不会在它应该运行的时候运行。我把它放在正文中,在提交按钮之前,但页面一开始就运行该功能。

我想checkForm在最后运行以验证数据的输入,但是每当我提交表单时,页面就会刷新。

我的函数是在错误的地方定义或调用的吗?我需要进行哪些更改才能停止页面刷新并让我的函数验证表单数据?

<div id="mc_embed_signup">
  <form id="form" method="post" onsubmit="return checkForm(this)">

  <script>
    function checkForm(form)
    if(radioValue = checkRadio(form.radiofield)) {
      return true;
    else {
      alert("Seleccione un tipo de operación");
      return false;
    }
    return true;        
  </script>

标签: javascripthtml

解决方案


提交时可以使用preventDefault()JS防止刷新

有关更多详细信息-> 转到此处:https ://www.w3schools.com/jsref/event_preventdefault.asp


推荐阅读