[英]Form still submits even though validation returns false
When I use the generic alert box the form validation works ok but if I try to make the alert box look nice using a JQuery script the form validates and submits despite the value returned being false 当我使用通用警报框时,表单验证可以正常工作,但是如果我尝试使用JQuery脚本使警报框看起来不错,尽管返回的值为false,表单仍会进行验证并提交
The standard alert box which works 可以正常使用的标准警报框
function validate(){
if( document.audit_form.audit_name.value == "" )
{
alert( "Please select a name for your audit" );
document.audit_form.audit_type.focus() ;
return false;
}
return true;
}
Attempt one with jQuery MsgBox from codecanyon 使用Codecanyon的jQuery MsgBox尝试一个
function validate(){
if( document.audit_form.audit_name.value == "" )
{
$.msgbox("Please select an audit type",
{
type:"alert",
buttons: [
{type: "submit", value: "Ok"}
]
},
function(result){
if (result && result=='Ok'){
return false;
}
});
}
return true();
}
Attempt two with Apprise 用Apprise尝试两次
function validate(){
if( document.audit_form.audit_name.value == "" )
{
apprise('Please enter a name for your audit?', {'textOk' : 'Ok',}, function(r) {
if(r) {
return( true );
} else {
return (false);
}
});
}
}
In all three instances the alert box displays when it should but with the JQuery versions the form still submits. 在所有这三种情况下,警报框都会显示应有的时间,但仍使用JQuery版本显示表单。 The functions are called using 这些函数使用
<form id="audit_form" name="audit_form" method="post" enctype="multipart/form-data"
action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return validate();">
Both $.msgbox
and apprise
are not blocking functions and the result is returned before functions' callback, you should change your logic. 无论$.msgbox
和apprise
不会阻止功能,并返回结果之前的功能回调,你应该改变你的逻辑。 Basically that's how your form submit should look like: 基本上,这就是你如何提交表单应该是这样的:
$('#audit_form').submit(function(event){
event.preventDefault();
yourUIAlertFunction(..., ..., function(isSuccess){
if(isSuccess){
submitFormLogic();
} else {
hideAlertAndShowErrors();
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.