这是我问题的完整来源。我无法在errorContainer中显示错误消息和元素的列表。即如果我还没有完成Address1和Address2,我很难在errorContainer中声明这一点?这可能吗? 非常感谢, 詹姆斯
$(function () {
var validator = $("#mytestform").validate({
rules: {
"data.Telephone": { number: true },
"data.Mobile": { number: true },
"data.Address1": { number: true },
"data.Address2": { number: true }
},
messages: {
"data.Telephone": "Please enter telephone number",
"data.Mobile": "Please enter mobile number",
"data.Address1": "Please enter address 1",
"data.Address2": "Please enter address 2"
},
errorContainer: $('#errorContainer'),
invalidHandler: function () {
$("#errorContainer").addClass(' error').text(validator.numberOfInvalids() + " field(s) are invalid");
}
});
});
</script>
<style type="text/css">
label.error { float: none; color: red; padding-left: .5em; vertical-align: top; }
</style>
</head>
<body>
<form id="mytestform" method="get">
<input name="data.Telephone" id="data.Telephone" class="required" />
<br/>
<input name="data.Mobile" id="data.Mobile" class="required" />
<br/>
<input name="data.Address1" id="data.Address1" class="required" />
<br/>
<input name="data.Address2" id="data.Address2" class="required" />
<br/>
<input type="submit" value="GO"/>
<br/><br/>
<div id="errorContainer">
</div>
</form>
答案 0 :(得分:0)
这是解决方案。
invalidHandler: function () {
$('#errorContainer').html('<ul>').addClass('errorContainer');
for (var i = 0; i < validator.errorList.length; i++) {
$('#errorContainer').append('<li>' + validator.errorList[i].message + '</li>');
}
$('#errorContainer').appendTo('</ul>');
},