本文介绍了jQuery验证-更改一个字段后可以重新验证一组字段吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jQuery Validate ,并希望重新验证一组字段每当其中之一更改时(或者可能在其中任何一个成功验证时).到目前为止,我的尝试只是创建无限循环.

I'm using jQuery Validate and would like to re-validate a group of fields whenever one of them is changed (or possibly, whenever one of them validates successfully). My attempts so far just create infinite loops.

这是可能的,还是插件设计排除了它?

Is this possible, or does the plugin design preclude it?

(具体来说,我已经,这是真的,我想对所有这些字段进行重新验证.目前,我正在清除它们的错误邮件带有我自己的代码,但这是一个技巧-在重新提交表单之前,它还会隐藏无关的验证问题.)

(Specifically, I've got a method that requires at least X of group Y to be filled out, and as soon as that's true, I'd like all those fields to re-validate. Currently I'm clearing out their error messages with my own code, but that's a hack - it also hides unrelated validation problems until the form is re-submitted.)

推荐答案

validate方法具有很少有选项可支持对更改进行重新验证,即:

The validate method has a few options to support re-validating on change, namely these:

$(".selector").validate({
  onfocusout: true,
  onkeyup: true,
  onclick: true,
  //The rest of your options
});

这些均默认为false,但应提供您在问题中提到的功能.

These all default to false, but should offer the functionality you mention in the question.

基于评论的更新:给出了一个简单的测试表单,如下所示:

Update based on comments: Given a simple test form like this:

<form action="get">
    <div><input type="text" name="part1" class="part"></div>
    <div><input type="text" name="part2" class="part"></div>
    <div><input type="text" name="part3" class="part"></div>
    <div><input type="text" name="part4" class="part"></div>
    <input type="submit" value="Submit" />
</form>

jQuery如下:

jQuery.validator.addMethod("require_from_group", function(value, element, options) {
    var valid = $(options[1], element.form).filter(function() {
        return $(this).val();
    }).length >= options[0];

    if(!$(element).data('reval')) {
        var fields = $(options[1], element.form);
        fields.data('reval', true).valid();
        fields.data('reval', false);
    }
    return valid;
}, jQuery.format("Please fill out at least {0} of these fields."));

$("form").validate({
    rules: {
        part1: { require_from_group: [2,".part"] },
        part2: { require_from_group: [2,".part"] },
        part3: { require_from_group: [2,".part"] },
        part4: { require_from_group: [2,".part"] }
    }
});​

您可以在此处进行演示,看看这是否是您想要的: http://jsfiddle.net /mhmBs/

You can play with a demo here, see if this is what you're after: http://jsfiddle.net/mhmBs/

此处的方法使用.data()来让元素知道不引起无限循环.正在模糊处理(验证触发器的正常原因)时,正在编辑的实际元素仅对您在组中指定的选择器中的其他元素进行重新验证,因此不对整个表单(如我的注释)进行重新验证. .it仅在最少数量的元素上触发它.

The method here uses .data() to let an element know not to cause an infinite loop. The actual element that is being edited, on blur (normal cause for validation triggers) re-validates only the other elements in the selector you specified in the group, so not the whole form like my comment...it's only triggering it on the minimum number of elements.​

这篇关于jQuery验证-更改一个字段后可以重新验证一组字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 09:07