我搜索过类似的问题并没有找到任何快乐。验证在send函数中的逻辑方面有效,但验证元素后面的范围在无效时不显示错误消息。我甚至试过传递errorPlacement,但唯一有效的是如果我特意设置它就好了
$("#msgvalid").text($("#msg").attr("data-val-required"));
相关的html是
<form id="frmSend" style="margin-top:15px">
Add Comment:
<div>
<textarea id="msg" data-val="true" data-val-required="The comment cannot be blank." placeholder="Enter comment here" style="height:50px;width:300px"></textarea>
<span id="msgvalid" class="field-validation-valid text-danger" data-valmsg-for="msg" data-valmsg-replace="true"></span>
</div>
<button type="button" onclick="send();" class="btn-default" style="margin-top:10px">Send Comment</button>
</form>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
和相关的javascript是
<script type="text/javascript">
function send() {
$("#frmSend").validate();
if (!$("#frmSend").valid()) { return false; }
alert("Would send");
}
</script>