jQuery验证插件 - 焦点事件与键/鼠事件不一致 [英] jQuery validation plugin - focus event inconsistent on key/mouse events

查看:62
本文介绍了jQuery验证插件 - 焦点事件与键/鼠事件不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用查询验证插件(http://docs.jquery.com/Plugins/Validation),我的目标是在提交表单之前显示错误消息。要做到这一点,我使用.valid()方法。

I'm using the query validation plugin (http://docs.jquery.com/Plugins/Validation) and my goal is to show error messages before the form is submitted. To do this I'm using the .valid() method.

我不想在出现错误之前显示这些消息,所以我先隐藏它们。当第一次聚焦该字段时,我不想显示错误消息。使用鼠标聚焦输入时效果很好,但是当您使用Tab键聚焦字段时,无论如何都会显示错误信息。

I don't want to show these messages until there's an error, so I'm hiding them first. When the field is focused the first time, I don't want to show an error message. This works great when focusing the input with the mouse, however when you use the tab key to focus the field the error message is shown anyway.

我创造了一个小提琴这里是为了展示焦点问题: http://jsfiddle.net/tucsonlabs/WQnHN/4/

I've created a fiddle here to demonstrate the focus problem: http://jsfiddle.net/tucsonlabs/WQnHN/4/

推荐答案

我需要使用@mindandmedia和@Fabian代码的组合。

I needed to use a combination of the code from @mindandmedia and @Fabian.

这对我有用:

var code; 

$('input').keyup(function(e) {
    code = (e.keyCode ? e.keyCode : e.which);
    if(code == 9) {
        return false;
    }
});

$('#myForm').validate();

$('input').blur(function() {
    if( !$(this).valid() ) { 
         $(this).next('label.error').show();                
    }
});

这篇关于jQuery验证插件 - 焦点事件与键/鼠事件不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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