jQuery 验证以忽略隐藏字段,同时仍然验证折叠面板隐藏的字段
jQuery Validate to ignore hidden fields while still validating fields hidden by a collapsed panel
首先对长标题感到抱歉当用户折叠面板时使用 jQuery 克隆 + 使用我当前的代码验证当我单击下一步按钮时它正在完美验证但在面板中我有一个字段这是隐藏的,当用户单击下一步按钮时不应验证。我使用了 ignore: []
代码,因此它适用于折叠字段但不适用于隐藏字段。当用户处于折叠模式和隐藏字段时,我希望两者都能工作。
这是jquery代码
$(".educationForm").validate({
ignore:[],
onkeyup: false,
showErrors: function (errorMap, errorList) {
var errors = this.numberOfInvalids();
if (errors) {
var message = errors === 0 ? 'You missed 1 field. It has been highlighted' : 'You have missed ' + errors + ' fields. Please fill before submitted.';
$("#error_message").html(message);
$(".error_msge").show();
} else {
$(".error_msge").hide();
}
this.defaultShowErrors();
},
errorPlacement: function () {
return false;
},
highlight: function (element) {
if ($(element).is(':radio')) {
} else {
$(element).addClass('errRed');
}
$(element).prevAll('label').find('span.required-star').addClass('text-error-red').removeClass('text-error-black');
},
unhighlight: function (element) {
if ($(element).is(':radio')) {
} else {
$(element).removeClass('errRed');
}
$(element).prevAll('label').find('span.required-star').addClass('text-error-black').removeClass('text-error-red');
}
});
add_validation_for_forms();
}
如果您想验证“隐藏”字段(被折叠的面板隐藏)而不验证其他隐藏字段,那么您只需将 ignore
选项设置为更具体的内容。
将名为 .ignore
的新 class 添加到您的隐藏字段。然后将 ignore
选项设置为 ".ignore"
。当然,您可以使用任何 class 或 jQuery 选择器。
ignore: ".ignore"
现在折叠的面板仍将接受验证,而隐藏字段将被忽略。
演示:http://jsfiddle.net/yg97yg19/22/
Elements to ignore when validating, simply filtering them out. ...
Example: Ignores all elements with the class "ignore" when validating.
$("#myform").validate({
ignore: ".ignore"
});
首先对长标题感到抱歉当用户折叠面板时使用 jQuery 克隆 + 使用我当前的代码验证当我单击下一步按钮时它正在完美验证但在面板中我有一个字段这是隐藏的,当用户单击下一步按钮时不应验证。我使用了 ignore: []
代码,因此它适用于折叠字段但不适用于隐藏字段。当用户处于折叠模式和隐藏字段时,我希望两者都能工作。
这是jquery代码
$(".educationForm").validate({
ignore:[],
onkeyup: false,
showErrors: function (errorMap, errorList) {
var errors = this.numberOfInvalids();
if (errors) {
var message = errors === 0 ? 'You missed 1 field. It has been highlighted' : 'You have missed ' + errors + ' fields. Please fill before submitted.';
$("#error_message").html(message);
$(".error_msge").show();
} else {
$(".error_msge").hide();
}
this.defaultShowErrors();
},
errorPlacement: function () {
return false;
},
highlight: function (element) {
if ($(element).is(':radio')) {
} else {
$(element).addClass('errRed');
}
$(element).prevAll('label').find('span.required-star').addClass('text-error-red').removeClass('text-error-black');
},
unhighlight: function (element) {
if ($(element).is(':radio')) {
} else {
$(element).removeClass('errRed');
}
$(element).prevAll('label').find('span.required-star').addClass('text-error-black').removeClass('text-error-red');
}
});
add_validation_for_forms();
}
如果您想验证“隐藏”字段(被折叠的面板隐藏)而不验证其他隐藏字段,那么您只需将 ignore
选项设置为更具体的内容。
将名为 .ignore
的新 class 添加到您的隐藏字段。然后将 ignore
选项设置为 ".ignore"
。当然,您可以使用任何 class 或 jQuery 选择器。
ignore: ".ignore"
现在折叠的面板仍将接受验证,而隐藏字段将被忽略。
演示:http://jsfiddle.net/yg97yg19/22/
Elements to ignore when validating, simply filtering them out. ...
Example: Ignores all elements with the class "ignore" when validating.
$("#myform").validate({
ignore: ".ignore"
});