本文介绍了消息提示onsubmit返回验证,但表单仍提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些与此参考非常相似的问题 onsubmit返回false无效,表单仍提交吗?我已经遵循了解决方案,但是如果将其更改为onclick,它将无法正常工作,甚至都不会提示任何消息.

I am having some issues which is quite similar referring to this onsubmit return false does not work, form still submits?I've followed the solution but if I changed it to onclick, it won't work, not even a message prompts out.

我正在使用的脚本.

<script>
function validate()
{
  var validate = false;
  if(document.getElementById("nameId").value == "")
  {  
    alert("NAME IS REQUIRED");
    validate   = false
  }

return true ;
}

</script>

我的代码附在下面.

<table class="<?php echo $table_style_3;?>" style='width: auto;'>
<form action="fhd_add_type.php" method="post" class="form-horizontal"   onsubmit="return validate()"> //This is the part where i've changed to onclick
<input type='hidden' name='nacl' value='<?php echo $nacl;?>'>
<input type='hidden' name='type' value='<?php echo $type;?>'>
<?php
if ($type <> 0) { ?>
    <tr><td>Name: </td><td><input type='text' name='type_name' id = "nameId"></td></tr> //This is the field where need to be validate
    <tr><td colspan='2'><input type='submit' value='add' class='btn btn-primary'></td></tr>
    </table>
<?php  }
if ($type == 0) { ?>
    <tr><td>Name</td><td><input type='text' name='type_name' id = "nameId"></td></tr> //This is the field where need to be validate
    <tr><td>Email</td><td><input type='text' name='type_email'></td></tr>
    <tr><td>Location</td><td><input type='text' name='type_location'></td></tr>
    <tr><td>Phone</td><td><input type='text' name='type_phone'></td></tr>
    <tr><td colspan='2'><input type='submit' value='add' class='btn btn-primary'></td></tr>
    </table>
<?php }?>
</form>
</table>

推荐答案

我认为这是因为您总是返回true.在您的JS中,尝试以下操作:

I think this is because you're always returning true. In your JS, try this:

<script>
function validate()
{
  var validate = true;
  if(document.getElementById("nameId").value == "") {  
       alert("NAME IS REQUIRED");
       validate = false;
    }

    return validate;
}
</script>

这篇关于消息提示onsubmit返回验证,但表单仍提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 02:09