jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery Validate – “跳过这些字段,或至少填写其中的X”大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在表格上使用了 jQuery Validation plugin.我有一些可选的字段组,但需要“全部或全部” – 如果您在组中填写一个输入,则必须填写所有字段.

例如,假设用户可以输入一个位置 – 街道,城市,州和邮政 – 或多个位置.您不必进入第二个位置,但如果您这样做,那么仅仅给这个城市是不行的;我也需要那个状态和拉链.

为了解决这个问题,我编写了一个自定义规则 – 实际上只是my previous rule一个小调整,require_from_group.这个叫做skip_or_fill_minimum.这是你如何使用它:

var validationrules = {
  rules: {
    LOCATIOn2address: {
    skip_or_fill_minimum: [4,'.LOCATIOn2']
  }
  //This input will validate if all 4 `.LOCATIOn2` inputs are filled,//or if all of them are left blank
}

var validator = $("#formtovalidate").validate(validationrules);

这是自定义规则的代码

jQuery.validator.addMethod("skip_or_fill_minimum",function(value,element,options) {
    var numberrequired = options[0];
    var SELEctor = options[1];
    //Look for our SELEctor within the parent form
    var numberFilled = $(SELEctor,element.form).filter(function() {
         // Each field is kept if it has a value
         return $(this).val();
      }).length;
      var valid = numberFilled >= numberrequired || numberFilled === 0;

    //The elegent part - this element needs to check the others that match the
    //SELEctor,but we don't want to set off a FeedBACk loop where all the
    //elements check all the others which check all the others which
    //check all the others...
    //So instead we
    //  1) Flag all matching elements as 'currently being validated'
    //  using jQuery's .data()
    //  2) Re-run validation on each of them. Since the others are Now
    //     flagged as being in the process,they will skip this section,//     and therefore won't turn around and validate everything else
    //  3) Once that's done,we remove the 'currently being validated' flag
    //     from all the elements
    if(!$(element).data('being_validated')) {
      var fields = $(SELEctor,element.form);
      //.valid() means "validate using all applicable rules" (which includes this onE)
      fields.data('being_validated',truE);
      fields.validate(); //Changed from fields.valid();
      fields.data('being_validated',falsE);
    }
    return valid;
    // {0} below is the 0th item in the options field
    },jQuery.format("Please either skip these fields or fill at least {0} of them."));

我主要发布这个帖子,以便搜索网络搜索解决方案的其他人可以找到它.仍然 – 有没有人看到改善这种情况的方法

更新 – 现在是jQuery验证的一部分

这是2012年4月3日的officially added to jQuery Validation.

解决方法

我的缩短和“优化”版本(更多链接使用验证器自定义提供的选择器).并希望减少选择操作.

仍然遭受你的规则所做的相同的错误 – 我的意思是删除错误类和所有符合我们的选择器的元素上的错误标签是不正确的.想象一下,您需要设置4个字段,但有5个字段.用户填写4个字段.第五个他没有填充,但在那个领域,你另外设置必要和电子邮件.这样,电子邮件错误消息也会被删除,尽管他还没有解决它.

jQuery.validator.addMethod("skip_or_fill_minimum",options) {
    var elems = $(element).parents('form').find(options[1]);
    var numberFilled = elems.filter(':filled').size();
    if (numberFilled >= options[0] || numberFilled == 0) {
        elems.removeClass('error');
        elems.next('label.error').not('.checked').text('').addClass('checked');
        return true;
    } else {
        elems.addClass('error');
    }
},jQuery.format("Please either skip these fields or fill at least {0} of them."));

大佬总结

以上是大佬教程为你收集整理的jQuery Validate – “跳过这些字段,或至少填写其中的X”全部内容,希望文章能够帮你解决jQuery Validate – “跳过这些字段,或至少填写其中的X”所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。