我已经通过用我自己的覆盖 jQuery 验证器中的 showErrors 函数解决了这个问题,该函数与不显眼的生成验证跨度兼容,并清理了具有无效类的有效字段。这不是很好的解决方法,但它有效。
这是带有解决方案的 jsfiddle:http: //jsfiddle.net/goranobradovic/ughCm/5/
更新:根据站点指南,由于指向外部站点的链接不是正确的答案,因此我在此处添加代码示例。对于已经熟悉 jQuery 验证的任何人,只需查看 showErrors 函数中的两行代码。我用 validator.settings.showErrors = showErrors; 将它分配给验证器。
HTML:
<form id="experiment" action="/" method="post">
<fieldset>
<legend></legend>
<div class="editor-label">
<label for="Email">Email</label>
</div>
<div class="editor-field">
<input data-val="true" data-val-email="&#39;Email&#39; not valid email address." data-val-required="&#39;Email&#39; is mandatory." id="Email" name="Email" type="text" value=""><span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="FirstName">First name</label>
</div>
<div class="editor-field">
<input class="text-box single-line" id="FirstName" name="FirstName" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="FirstName" data-valmsg-replace="true"></span>
</div>
<div class="editor-label">
<label for="LastName">Last name</label>
</div>
<div class="editor-field">
<input class="text-box single-line" id="LastName" name="LastName" type="text" value="">
<span class="field-validation-valid" data-valmsg-for="LastName" data-valmsg-replace="true"></span>
</div>
</fieldset>
<p>
<button type="submit" class="save ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-secondary" value="Save" role="button" aria-disabled="false"><span class="ui-button-text">Save</span><span class="ui-button-icon-secondary ui-icon ui-icon-disk"></span></button>
</p>
</form>
<br/>
<button id="add">Add error</button>
<button id="remove">Remove error</button>
<br/>
<br/>
Debug:
<div id="debug"></div>
JavaScript:
var validator = {};
function addError(e) {
validator.showErrors({
"FirstName": "test error"
});
}
function removeError(e) {
validator.showErrors({
"FirstName": null
});
fixValidFieldStyles($("form"), validator);
}
$(document).ready(function() {
var $form = $("#experiment");
// prevent form submission
$form.submit(function(e) {
e.preventDefault();
return false;
});
$("#add").click(addError);
$("#remove").click(removeError);
$("#debug").html("<h1>Validator properties:</h1>");
validator = $form.validate();
validator.settings.showErrors = showErrors;
for (var i in validator) {
var row = $("<span></span>").html(i).append("<br/>");
$("#debug").append(row);
}
});
function showErrors(errorMessage, errormap, errorlist) {
var val = this;
errormap.forEach(function(error, index) {
val.settings.highlight.call(val, error.element, val.settings.errorClass, val.settings.validClass);
$(error.element).siblings("span.field-validation-valid, span.field-validation-error").html($("<span></span>").html(error.message)).addClass("field-validation-error").removeClass("field-validation-valid").show();
});
}
function fixValidFieldStyles($form, validator) {
var errors = {};
$form.find("input,select").each(function(index) {
var name = $(this).attr("name");
errors[name] = validator.errorsFor(name);
});
validator.showErrors(errors);
var invalidFields = $form.find("." + validator.settings.errorClass);
if (invalidFields.length) {
invalidFields.each(function(index, field) {
if ($(field).valid()) {
$(field).removeClass(validator.settings.errorClass);
}
});
}
}