jquery validate - 验证页面加载时的字段

发布于 2024-08-25 16:35:41 字数 125 浏览 3 评论 0原文

我的纯 html 表单上有一个输入字段,我希望从页面加载的那一刻起将其标记为无效(并显示错误消息)。

这可能与 jquery 验证插件有关吗?

如果做不到这一点,当插件尝试验证特定输入字段时如何隐藏元素?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身以后 2024-09-01 16:35:41

我认为您必须在页面加载时提交正在验证的表单。大概是这样的:

   $(document).ready(function() {
       $('form').validate(/* set all of your validator options here */);
       $('form').trigger('submit');
    });

I think you would have to have the form you are validating be submit on the page load. Probably something like this:

   $(document).ready(function() {
       $('form').validate(/* set all of your validator options here */);
       $('form').trigger('submit');
    });
彡翼 2024-09-01 16:35:41

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

我使用验证方法的突出显示取消突出显示选项来实现我想要的效果。

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

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

也许这会帮助其他人将来摆脱困境。

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文