错误的jquery验证和真实的验证会使chrome文本区域失去焦点

false jquery validation followed by true validation makes chrome text area lose focus

本文关键字:验证 文本 区域 失去 chrome 焦点 jquery 真实 错误      更新时间:2023-09-26

我在一个带有文本区域的表单上使用jquery验证。表单中的html如下所示(不包括验证fine的其他字段):

<form id="contact" name="contact" action="/forms/contact_received.php" method="get">
    <textarea name="message" cols="60" rows="10" value=""></textarea>
</form> 

jquery脚本如下所示:

$("#contact").validate({    
    rules: { 
        name: "required",
        email: {required: true, email: true},
        subject: "required",
        message: {required: true}
    },
    messages: { 
        name: "Enter your name", 
        email: {required: "Please enter a valid email address",minlength: "Please enter a valid email address"},
        subject: "it's required",
        message: "Please enter your message in the box above"
    }
});

在Chrome中,当用户在该文本区域为空时尝试提交时,会出现我的错误消息。

然后,当用户在文本区域输入文本时,他可以写一个字符(显示为红色的"错误"文本,然后在第一个字符后变为黑色),然后错误消息消失,失去对文本区域的关注,文本区域回到其初始宽度。然后,用户必须再次单击表单才能输入其余的注释。

如何在验证返回"true"后保持对文本区域的关注?

我想明白了。问题是我在站点范围的css中设置了.error{display:block}。也许我应该删除这个,但有几个人在jquery论坛上也遇到了同样的问题,所以这是一个合法的问题:

http://forum.jquery.com/topic/jquery-validate-in-chrome-field-loses-focus-on-unhighlight