在多年尝试避免JavaScript之后,Iv开始在MVC asp.net中使用JQuery for
validation,因为似乎没有正式的验证方式,我很惊讶,JQuery是多么的好.
首先有没有办法让intellisense工作的JQuery和它的验证插件,所以我不必学习api?
其次,如何为此创建验证摘要,它现在将错误附加到文本框的右侧:
<script type="text/javascript"> $().ready(function() { $("#CreateLog").validate({ rules: { UserName: { required: true,minLength: 2,} },messages: { UserName: { required: "Please enter a username",minLength: "Your username must consist of at least 2 characters",} } }); }); </script> <form id="CreateLog" action="Create" method="post" /> <label>UserName</label><br /> <%=Html.TextBox("UserName")%> <br /> <div class="error"> </div> <input type=submit value=Save /> </form>
我尝试将此添加到脚本中:
errorLabelContainer: $("#CreateLog div.error")
而这到html:
<div class="error"> </div>
但这没有办法.
解决方法
尝试在选项中指定包装器和标签容器.我还添加了display:none;以错误容器的风格让jquery决定何时显示它.
HTML:
<div class="error-container"> <ul> </ul> </div> <form id="CreateLog" action="Create" method="post" /> <label>UserName</label><br /> <%=Html.TextBox("UserName")%> <br /> <input type=submit value=Save /> </form>
脚本:
<script type="text/javascript"> $().ready(function() { $("#CreateLog").validate({ errorLabelContainer: $("ul",$('div.error-container')),wrapper: 'li',rules: { UserName: { required: true,} },messages: { UserName: { required: "Please enter a username",} } }); }); </script>
这应该工作.