我有一个带有两个输入文本框的表单,并且我已经包含了两个输入文本框的 jQuery 验证规则:
I have a form with two input textboxes, and I have included jQuery validation rules for both:
<script src="../../Scripts/jquery-validate/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#respondForm').validate({ onclick: false,
onkeyup: false,
onfocusout: false,
highlight: function(element, errorClass) {
$(element).css({ backgroundColor: 'Red' });
},
errorLabelContainer: $("ul", $('div.error-container')),
wrapper: 'li',
rules: {
'text': {
required: true,
minlength: 5,
maxlength: 10
},
integer: {
required: true,
range: [0, 90]
}
},
messages: {
'text': {
required: "xxx_Required",
minlength: "XXX Should be greater than 5",
maxlength: "XXX Cannot be greater than 10"
},
integer: {
required: "is required",
range: "is out of range: [0,90]"
}
}
});
});
</script>
</head>
.
.
.
<input type="text" id="text" name="text" />
<br />
<input type="text" id="integer" name="integer" />
<br />
<input type="submit" name="submit" value="Submit" />
<br />
我用过:
function(element, errorClass) {
$(element).css({ backgroundColor: 'Red' });
}
突出显示错误控制.现在的问题是,在以下场景中,两个输入文本框都保持突出显示(背景颜色:红色):
to highlight the error control. Now the problem is that in the following scenario, both the input textboxes remain highlighted (background color: red):
我该如何解决这个问题?
How do I resolve this problem?
找到答案,您还必须提供 unhighlight
属性.
Found the answer, you have to provide an unhighlight
property as well.
将错误类添加到无效元素及其标签
$(".selector").validate({
highlight: function(element, errorClass) {
$(element).addClass(errorClass);
$(element.form).find("label[for=" + element.id + "]")
.addClass(errorClass);
},
unhighlight: function(element, errorClass) {
$(element).removeClass(errorClass);
$(element.form).find("label[for=" + element.id + "]")
.removeClass(errorClass);
}
});
更多信息
这篇关于JQuery 验证插件 - 错误高亮问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!