我以前有使用过这个插件的经验。据我所知,试试这个,
<script id="demo" type="text/javascript">
jQuery(document).ready(function($) {
var validator = $("#your_form_id").validate({
rules: {
fn: "required",
ln: "required",
em: {
required: true,
email: true,
},
},
messages: {
fn: "Please enter your first name.",
ln: "Please enter your last name.",
em: {
required: "Please enter your email address.",
email: "Please enter a valid email address.",
},
},
errorPlacement: function(error, element) {
error.appendTo( element.parent().next() );
},
success: function(label) {
label.html("OK").addClass("checked");
}
});
});
</script>
在你的 HTML 中;
<input type="text" name="fn" value="" class="required" title="Please enter your first name." id="fn"><span class="status"></span><br>
<input type="text" name="ln" value="" class="required" title="Please enter your last name." id="ln"><span class="status"></span><br>
<input type="text" name="em" value="" class="required" id="em"><span class="status"></span><br>
<span class="status">
这将在相应的输入元素附近显示错误消息。还可以使用 CSS 设置您的success
和error
消息的样式;
#your_form_id label.error {
background:url("error.png") no-repeat 0px 0px;
color: #CC0000;
}
#your_form_id label.checked {
background:url("checked.png") no-repeat 0px 0px;
color: #008000;
}
#your_form_id .success_msg {
color: #008000;
}
如果你仍然想使用Meta
,下面是完整的代码,对我来说很好
<head>
<script src="jquery-1.6.1.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
<script src="jquery.metadata.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#commentForm").validate({meta: "validate"});
});
</script>
</head>
<body>
<form id="commentForm" method="post" action="test.php">
<input id="fn0" type="text" name="fn" value="" class="required" title="Please enter your first name."><br />
<input id="ln1" type="text" name="ln" value="" class="required" title="Please enter your last name."><br />
<input id="em2" type="text" name="em" value="" class="{validate:{required:true, email:true, messages:{required:'Please enter your email address', email:'Please enter a valid email address'}}}"/><br />
<input class="submit" type="submit" value="Submit"/>
</form>
</body>
希望这可以帮助。