2011-11-08 16 views
8

我想获得jquery验证插件与onkeyup或onfocusout选项一起工作。每次添加这些选项并触发其中一个选项时,我都会遇到错误。如果我提交我的表单,验证将起作用。f.settings [g] .call与任何东西都不是一个函数,但提交验证

我真的不允许发布我正在处理的表单,但是我用一个非常简单的表单创建了类似的问题,我只是在子目录static/js /下的所有j目录中加载了一个目录。

我使用jQuery 1.6.2和jQuery验证1.9.0

任何人有什么想法?

<html> 
<script type="text/javascript" src="static/js/jquery.js"></script> 
<script type="text/javascript" src="static/js/jquery.validate.js"></script> 

<script type="text/javascript"> 
$(document).ready(function(){ 

    var validator = $('#submitform').validate({ 
     rules: { 
      name: 'required', 
      phone: { 
       required: true, 
       minlength: 12 
      }, 
      team: { 
       required: true, 
       minlength: 1 
      }, 
      fax: { 
       required: true, 
       minlength: 12 
      } 
     }, 
     messages: { 
      name: 'Your name is required', 
      phone: 'Your phone number is required', 
      team: 'Your extension number is required', 
      fax: 'Your fax number is required' 
     }, 
     // the errorPlacement has to take the table layout into account 
     errorPlacement: function(error, element) { 
      if (element.is(":radio")) 
       error.appendTo(element.parent().next().next()); 
      else if (element.is(":checkbox")) 
       error.appendTo (element.next()); 
      else 
       error.appendTo(element.parent()); 
     }, 
     onfocusout: true, 
     // specifying a submitHandler prevents the default submit, good for the demo 
     submitHandler: function() { 
      alert("submitted!"); 
     }, 
     // set this class to error-labels to indicate valid fields 
     success: function(label) { 
      // set as text for IE 
      label.html(" ").addClass("checked"); 
     } 
    }); 
}); 
</script> 

<form id="submitform" action="."> 

<label for="name">Your Name:</label> 
<input id="ins_name" type="text" name="name" maxlength="40" /> 

<label for="phone">Phone:</label> 
<input name="phone" maxlength="14" type="text" id="phone" /> 

<label for="extension">Extension:</label> 
<input name="extension" maxlength="10" type="text" id="extension" /> 

<label for="fax">Fax:</label> 
<input name="fax" maxlength="14" type="text" id="fax" /> 

<input type="submit" value="Submit"> 
</form> 
+0

我会猜测onfocusout应该是一个函数。 – AutoSponge

+0

根据文档,它只是一个选项 - http://docs.jquery.com/Plugins/Validation/validate#toptions – grantk

+0

选项是配置对象的属性,它们的值可以是任何值。在这种情况下,我猜测这个类型应该是函数,因为它是用于事件处理程序的(它在jQuery中很常见),而且它没有很好的记录(像大多数插件)。 – AutoSponge

回答

14

如上所述,文档有缺陷。实际上,您需要将onfocusout选项设置为接受要验证的元素作为参数的函数。这里是工作的代码示例:

$("#form").validate({ 
    onfocusout: function(element) {$(element).valid()} 
}); 
1

在我的情况下,有一个线onkeyup: true然后我改成了false

onkeyup: false 

但我仍然不知道什么时候开始的错误来。 :)