1
如果验证失败,我使用jQuery验证来生成自定义消息。 默认情况下,消息位于正在验证的输入字段的右侧。 我希望此消息总是位于字段标题的右侧。如何将jQuery验证文本放置在标题的右侧而不是输入字段的右侧
举例来说,我希望它看起来像这样: http://jsfiddle.net/zWgbP/
我已经加入玩耍了:
errorPlacement: function (error, element) {
error.insertBefore(element);
},
...但是,这是不完全正确,因为我需要它在名字“Last Name”之后的休息之前。
有没有更好的方法来定位字段标题,如果我把它放在一个span或div中,并使用jQuery方法给它一个id我下面?
如果没有,是否有更好的方法?
JavaScript的
$(document).ready(function() {
// validate signup form on keyup and submit
$("#newform").validate(
rules: {
lastname: {
required: true,
minlength: 2
},
messages: {
lastname: {
required: "* please fix",
minlength: "* please fix"
},
的HTML
<form name="newform" id="newform" method="post" onsubmit="return OnNewButton();">
<input type='text' id='lastname' name='lastname' size='20' value = "" />
</form>
的CSS
<style type="text/css">
#newform label.error {
color: red;
font-size: 15px;
}
</style>
'errorPlacement'似乎是正确的解决方案,您只需要弄清楚正确的关系。您的HTML不包含输入字段的标签,因此很难说明它应该是什么。 – Barmar
Barmar是正确的,检查一些更多信息的选项页面http://docs.jquery.com/Plugins/Validation/validate#toptions – Jerryf
谢谢:看起来像appendTo选项是我所需要的。 $( “#myForm的”)来验证({ errorPlacement:功能(错误,元素){ error.appendTo(element.parent( “TD”)下( “TD”));} , 调试:真 }) – Chain