onkeyup和onfocusout在jQuery Validate中不起作用 [英] onkeyup and onfocusout is not working in jQuery Validate

查看:334
本文介绍了onkeyup和onfocusout在jQuery Validate中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery Validate插件进行验证.我必须对表单进行实时验证. 例如,如果需要,则用户应该第一次获得所需的消息,然后在开始在文本框中输入内容时就会消失.

I am using jQuery Validate plugin for the validation purpose. I have to real time validation on the form. For example if a first name is required, then user should get the required message for first time and goes when he start to type in the text box.

要实现此功能,我在插件的onkeyuponfocusout选项上使用. 为此,我引用了此链接谁有同样的问题.

To achieve this functionality I am using on onkeyup and onfocusout options of the plugin. For this I refer this link who has the same issue.

这是我的咖啡代码,用于表单验证

Here is my coffeescript code for form validation

defaultPageForm: (self)->
      form = $("#PageForm")
      if form.length > 0
        form.find('textarea').on 'click', -> $(@).valid()
        form.find('input').on    'click', -> $(@).valid()
        form.validate    

          ignore: ".ignore"                        
          rules:
            "view[name]":
              required: true
              NameWordCount: [3]


            "view[comment]":
              required: true
              CommentWordCount: [5]

            accept_terms: "required"

          messages:
            "view[comment]": "Please add comment"

          errorElement: "span"
          errorClass: "error_msg wide "
          errorPlacement: (error, element) ->
            element.siblings(".view_form_error_msg").remove()
            error.appendTo element.closest(".controls")              
            return

          ###### Real time validation code #############
          onkeyup: (element) -> 
            $(element).valid()  
            return        

          onfocusout: (element) -> 
            $(element).valid()  
            return 

          submitHandler: (form) ->
            self._setupData()
            self._submitForm(form)
            off

      return

但是它不能正常工作.例如,出于测试目的,我在onkeyup下添加了警报消息,但没有弹出任何警告消息,并且未应用实时验证.

But it not working fine. For example for testing purpose I added alert message under onkeyup but is nothing pop up also it not applying realtime validation.

有什么我想念的吗? 这是我的html

Is there anything I am missing. Here is my html

<input type="text" value="as as" size="50" placeholder="Full Name" name="view[name]" id="customerName" class="string required wide" style="font-size: 20.4572px; line-height: 49px;">

那我该如何解决这个问题.

So how can I resolve this issue.

推荐答案

您永远不会在.validate()方法中使用.valid()方法.请参考onkeyuponfocusout的源代码以了解其工作方式.

You would never use the .valid() method within the .validate() method. Refer to the source code of onkeyup and onfocusout to see how it's done.

使用this.element(element)代替$(element).valid()

要覆盖onkeyuponfocusout的默认功能(惰性验证"),而应使用渴望"验证...

To over-ride the default functionality ("lazy validation") of onkeyup and onfocusout and use "eager" validation instead...

$('#myform').validate({
    onkeyup: function(element) {
        this.element(element);  // <- "eager validation"
    },
    onfocusout: function(element) {
        this.element(element);  // <- "eager validation"
    }
    ....

演示: http://jsfiddle.net/ajy5j8jq/

DEMO: http://jsfiddle.net/ajy5j8jq/

这篇关于onkeyup和onfocusout在jQuery Validate中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆