jQuery validate-验证页面加载中的字段 [英] jquery validate - validating a field on pageload

查看:106
本文介绍了jQuery validate-验证页面加载中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在普通的html表单上有一个输入字段,从页面加载之时起,我就希望将其标记为无效(并显示错误消息).

I've got an input field on my plain html form that I would like to be marked as invalid (and the error message shown) from the moment the page loads.

这可能与jquery validate插件有关吗?

Is this possible to do with the jquery validate plugin?

如果失败,当插件尝试验证特定输入字段时如何隐藏元素?

Failing that, how can I hide an element when the plugin tries to validate a particular input field?

推荐答案

设法提出了一个解决方案.它涉及添加虚拟"验证元素,该元素在页面加载时显示.一旦用户在相应的字段中输入一个值,虚拟元素就会被隐藏并显示真实的元素.

Managed to come up with a solution. It involved adding a 'dummy' validation element that is shown when the page is loaded. As soon as the user inputs a value into the corresponding field, the dummy element is hidden and the real one is shown.

对于验证方法,我使用了 highlight unhighlight 选项来实现我想要的.

I used the highlight and unhighlight options for the validate method to achieve what i wanted.

$("#myform").validate({
    highlight: function(element){
        if($(element).hasClass("math")){
           $(".temp").hide();
        }
    },

    unhighlight: function(element){
        if($(element).hasClass("math")){
           $(".temp").hide();
        }
    }
});

也许这会在将来帮助别人.

Maybe this will help someone else out in future.

这篇关于jQuery validate-验证页面加载中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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