2011-11-23 46 views
5

我有一个有四个字段的表单。我已经对其中3个应用了一些不显眼的验证。 我希望当所有不显眼的验证已经执行,然后调用jquery函数,但我已经在窗体上定义了一个onsubmit事件,以便每次它首次进入该jquery函数并执行所有步骤,然后显示不显眼各个文本框的验证消息。如何检查不显眼的验证是否已在jquery函数中验证过?

但是我想执行jquery函数的所有步骤,如果表单通过了不显眼的验证。

有没有办法检查所有不显眼的验证是否已执行?

这是我的jQuery代码:

function submitDetails() 
{ 
var check = $('#flag').val();    
if (check == 1) 
{ 
return true; 
} 

else { 
var birthDate = ($('#BirthDate').val()).split('/'); 
var graduationDate = ($('#GraduationDate').val()).split('/'); 
var stdate = birthDate[2] + birthDate[1] + birthDate[0]; 
var endate = graduationDate[2] + graduationDate[1] + graduationDate[0]; 
if (($('#LastName').val() == "") || (parseInt(endate) < parseInt(stdate))) 
    { 
    $('#Window').data('tWindow').center().open(); 
    return false; 
} 
else { return true; } 
} 
} 

回答

10

你可以检查形式是在提交有效的处理程序:

$('#formId').submit(function() { 
    if (!$(this).valid()) { 
     // validation failed => here you can call your function or whatever 
     return false; 
    } else { 
     // the form is valid => you could perform some other action if you will 
    } 
}); 
+0

感谢达林, 我已经试过了,没有上调用任何函数onsubmit事件,但这里也首先检查不显眼的验证,当我点击提交,并且如果所有验证都满足,那么它将转到发布操作。不要像你所说的去jquery函数,在哪里需要检查一些步骤。 谢谢.. – Kriitika

+0

我给表单ID如下: // form body '{{Html.BeginForm(“Index”,“Home”,FormMethod.Post,new {id =“formId”});} @ {Html.EndForm();}' 并且在页面的开头定义了头部的jquery为你有什么建议 但是代码不会执行Jquery的步骤。 – Kriitika

相关问题