使用jQuery Validate插件验证表单的子集 [英] Validate subset of a form using jQuery Validate plugin

查看:68
本文介绍了使用jQuery Validate插件验证表单的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的HTML表单具有多个div,这些div是向导的步骤.单击下一步"按钮后,我只想验证活动的div.我正在使用jQuery.Validate.js插件.

My HTML form has a number of divs that are the steps of a wizard. Upon clicking a "next" button I want to validate just the active div. I'm using the jQuery.Validate.js plugin.

每个div都有一个ID,所以我想说一种类似的方式:

Each div has an ID, so I want a way to say something like:

wizardForm.validate().element('#first-step :input')

但是这仅验证第一个输入,而不是所有输入. 如何验证div中的所有输入?

but this only validates the first input, not all of them. How can I validate all inputs within a div?

推荐答案

根据jAndy的建议,我创建了此辅助函数:

Taking what jAndy suggested, I created this helper function:

jQuery.validator.prototype.subset = function(container) {
    var ok = true;
    var self = this;
    $(container).find(':input').each(function() {
        if (!self.element($(this))) ok = false;
    });
    return ok;
}

用法:

if (wizardForm.validate().subset('#first-step')) {
    // go to next step
}

这篇关于使用jQuery Validate插件验证表单的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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