我正在使用 jQuery Validation Engine(特别感谢他们的开发人员)。好的,问题如下:我需要设置一组必填字段,前提是至少填写其中一个。
我在_validateField
函数中添加了以下代码:
case "ifOneAllGroupRequired":
// Check is its the first of group, if not, reload validation with first field
// AND continue normal validation on present field
var classGroup = "["+options.validateAttribute+"*=" +rules[i + 1] +"]";
errorMsg = methods._ifOneAllGroupRequired(field, rules, i, options);
if(errorMsg) { required = true; }
options.showArrow = false;
break;
并制作了下一个功能:
_ifOneAllGroupRequired: function(field, rules, i, options) {
var classGroup = "["+options.validateAttribute+"*=" +rules[i + 1] +"]";
var isValid = 0 , getTotal = 0;
// Check all fields from the group
field.closest("form").find(classGroup).each(function(){
getTotal++; // count all the fields
if(! methods._required($(this), rules, i, options) ){
isValid++; // count the fields filled
}
});
// if at least 1 is filled and the other arent launch error
if( isValid != getTotal && isValid != 0 ) return options.allrules[rules[i]].alertText;
// if they are, clean all errors
else field.closest("form").find(classGroup).validationEngine('hide');
},
我得到了这个工作,问题是错误出现在每个字段之上,而不是像“groupRequired”选项(默认在validationEngine上)
我没有头绪,只好转身。有任何想法吗?