我在谷歌浏览器中运行正常的功能有问题,但在Mozilla中很糟糕:
JavaScript代码是:
<script type="text/javascript">
function gid(id) {
return document.getElementById(id);
}
function validate() {
var x = document.forms["frmSurvey"]["email"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if((gid('0_stars').checked==true) || (gid('1_stars').checked==true) || (gid('2_stars').checked==true) || (gid('3_stars').checked==true) || (gid('4_stars').checked==true)) {
if((gid('comment').value != '') && (gid('email').value != '') && (gid('name').value != '')) {
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
alert("Not a valid e-mail address. Please write a valid e-mail address!");
return false;
} else {
document.getElementById("frmSurvey").submit();
}
}
else {
alert('You haven`t completed the mandatory fields. Please make sure that you complete the form corectly! Thank you!');
return false;
}
}
else {
alert('You haven`t completed the mandatory fields. Please make sure that you complete the form corectly! Thank you!');
return false;
}
}
</script>
表格是:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post" id="frmSurvey" onsubmit="event.preventDefault(); validate();">
...
...
...
</form>
有没有方法可以用另一种方法替换这个功能?我已经尝试过返回false;但它根本没有提交!
答案 0 :(得分:1)
将return true
添加到validate
的末尾。
然后改变:
onsubmit="event.preventDefault(); validate();"
......来:
onsubmit="return validate();"
这将导致表单只有在经过验证后才能提交。
修改强>
完成此操作后,您可以从validate()
document.getElementById("frmSurvey").submit();
答案 1 :(得分:0)
尝试删除表单元素中的onsubmit并将其添加到js文件中。
$('#frmSurvey').submit(function( event ) {
event.preventDefault();
});