jQuery 在提交表单上,返回 false 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18480001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
On submit form, return false not working
提问by Nejc Galof
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.
当我提交表单时,我收到了一条警告消息。当我接受警报时,它无论如何都会提交表单。返回 false 将被忽略。Onclick 不能使用。我试过了var x = document.forms["form"]["fname"].value;
,还是一样。
<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>
回答by Aashray
Instead of <input type="submit" value="submit"/>
use <input type="button" value="Submit" onclick='validateForm()'/>
.
而不是<input type="submit" value="submit"/>
使用<input type="button" value="Submit" onclick='validateForm()'/>
.
In your JS:
在你的 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>
回答by Saranya Sadhasivam
Give this script inside of head tag and check it.
将此脚本放在 head 标签内并检查它。
<script type="text/javascript">
function validateForm() {
var x = document.getElementById('test').value;
if (x == null || x == 0 || x == "0") {
alert("Stop");
return false;
}
}
</script>