我试图用此模板包装错误标签,但没有成功。 (http://jqueryvalidation.org/)jQuery验证错误标签模板
<div class="alert alert-error">
<div class="fx-alert-icon">
<i class="fxicons fxicons-error"></i>
</div>
<div class="fx-alert-desc">
<label id="input-error" class="has-error" for="input1">
Input with error!!
</label>
</div>
</div>
一种解决方案是改变errorElement
然后在方法showErrors()
改变message
,但我不觉得舒服这种解决方案
var errMsgTmpl = '<div class="alert alert-error">' +
'<div class="fx-alert-icon">' +
' <i class="fxicons fxicons-error"></i>' +
'</div>' +
'<div class="fx-alert-desc"><label for="{{LABEL-FOR}}">{{ERROR-MSG}}</label></div>' +
'</div>';
$.validator.setDefaults({
errorElement: "section",
showErrors: function (errorMap, errorList) {
var i, elements;
for (i = 0; errorList[i]; i++) {
errorList[i].message = errMsgTmpl
.replace(/{{ERROR-MSG}}/, errorList[i].message)
.replace(/{{LABEL-FOR}}/, $(errorList[i].element).attr('id'));
}
this.defaultShowErrors();
}
});
在这一刻,我的布局工作没有模板,只使用CSS,但在某些时候我的信息可能会更复杂,我认为使用模板是最好的选择。
'showErrors'回调选项用于创建消息摘要框,而不是用于自定义单个消息。 – Sparky
谢谢@Sparky,一些想法如何定制个人消息? –