Jquery Ajax表单忽略验证
我以前很少使用ajax,所以选择了。 我的目标是让插件提交表单,使用我的验证检查表单字段是否正确填写,如果正确,请提交表单 上述所有工作,验证除外。当我单击“提交”时,验证会短暂显示,然后该框会淡出,正如我在ajax表单脚本的成功调用中设置的那样Jquery Ajax表单忽略验证,jquery,ajax,Jquery,Ajax,我以前很少使用ajax,所以选择了。 我的目标是让插件提交表单,使用我的验证检查表单字段是否正确填写,如果正确,请提交表单 上述所有工作,验证除外。当我单击“提交”时,验证会短暂显示,然后该框会淡出,正如我在ajax表单脚本的成功调用中设置的那样 var options = { beforeSubmit: validation, success: function() { var url = '/includes/embed-topbar-login-accou
var options = {
beforeSubmit: validation,
success: function() {
var url = '/includes/embed-topbar-login-account';
$('#account').load(url, function() {
$('#account-options').fadeOut('slow');
});
}
};
$('#login').ajaxForm(options);
function validation() {
var prompt = '<div class="required-prompt"><small>This field is required.</small></div>';
var promptEmail = '<div class="required-prompt"><small>Invalid email address.</small></div>';
var required = true;
$('.required-prompt', this).remove();
$('.required', this).each(function() {
if( !$(this).val() ) {
$(this).after(prompt);
required = false;
} else {
$('.required-prompt', this).remove();
}
});
return required;
}
var选项={
提交前:验证,
成功:函数(){
var url='/includes/embed topbar登录帐户';
$('#account').load(url,函数(){
$(“#帐户选项”).fadeOut('slow');
});
}
};
$('#login').ajaxForm(选项);
函数验证(){
var prompt='此字段是必需的';
var promptEmail='无效的电子邮件地址';
所需var=真;
$('.required prompt',this.remove();
$('.required',this).each(函数(){
if(!$(this.val()){
$(此).after(提示);
必需=假;
}否则{
$('.required prompt',this.remove();
}
});
要求归还;
}
是否有人能告诉我,如果验证返回“false”(当前为false),是否需要任何额外的步骤来确保提交停止。您应该在提交前指定
$('#login').ajaxForm({
success: function() {
var url = '/includes/embed-topbar-login-account';
$('#account').load(url, function() {
$('#account-options').fadeOut('slow');
});
},
beforeSubmit: function(arr, $form, options) {
// The array of form data takes the following form:
// [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
// return false to cancel submit
return validate(); //Assuming validate is your function doing validation.
}
});
您没有使用beforeSubmit对该代码调用validate方法。你读过插件文档的验证示例页面了吗?验证不是更像$('#form').validate()?我已经尝试在我的验证代码中使用before submit,它显示的与以前相同,并且仍然提交。你能在我的代码中找到原因吗?