在提交表单中,返回false不起作用

编程入门 行业动态 更新时间:2024-10-15 16:20:48
本文介绍了在提交表单中,返回false不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我提交表单时,我收到了一条警告消息。当我接受警报时,它仍然会提交表格。返回false将被忽略。 Onclick无法使用。我尝试使用 var x = document.forms [form] [fname] .value; 并且仍然相同。

< form id =fmethod =postname =formonsubmit =return validateForm(); 行动= # > < input type =textname =fnameid =test/> < input type =submitvalue =submit/> < / form> < script type =text / javascript> 函数validateForm(){ var x = document.getElementById('test')。value; if(x == null || x == 0 || x ==0){ alert(Stop); 返回false; } } < / script>

解决方案

替代< input type =submitvalue =submit/> use < input type =buttonvalue =Submitonclick ='validateForm()'/> 。

在您的JS中:

< script type =text / JavaScript的> 函数validateForm(){ var x = document.getElementById('test')。value; if(x == null || x == 0 || x ==0){ alert(Stop); } else document.form.submit(); } < / script>

When I submit the form I got an alert message. When I accept the alert it will submit the form anyway. Returning false is ignored. Onclick can not be used. I try with var x = document.forms["form"]["fname"].value; and still same.

<form id="f" method="post" name="form" onsubmit="return validateForm();" action="#"> <input type="text" name="fname" id="test" /> <input type="submit" value="submit"/> </form> <script type="text/javascript"> function validateForm() { var x = document.getElementById('test').value; if (x == null || x == 0 || x == "0") { alert("Stop"); return false; } } </script>

解决方案

Instead of <input type="submit" value="submit"/> use <input type="button" value="Submit" onclick='validateForm()'/>.

In your JS:

<script type="text/javascript"> function validateForm() { var x = document.getElementById('test').value; if (x == null || x == 0 || x == "0") { alert("Stop"); } else document.form.submit(); } </script>

更多推荐

在提交表单中,返回false不起作用

本文发布于:2023-11-24 01:16:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623440.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   不起作用   false

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!