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>
2 个解决方案
#1
9
Instead of <input type="submit" value="submit"/>
use <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>
#2
0
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>
#1
9
Instead of <input type="submit" value="submit"/>
use <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>
#2
0
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>