从锚标记触发 ASP.NET MVC2 客户端验证
我正在更改现有的 ASP.NET MVC2 应用程序,该应用程序使用 MicrosoftMvcValidation.js 等进行客户端验证。
<h2>Details</h2>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Details", "Add", FormMethod.Post, new { id="addForm" }))
{ %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Title) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Title) %>
<%: Html.ValidationMessageFor(model => model.Title) %>
</div>
...
<input type="submit" value="Create" />
客户端验证工作正常。
然而,设计者想把提交按钮改成一个漂亮的锚标签。我正在尝试使用锚点的 onclick 事件提交表单,即:
<a class="add-campaign-button-done" onclick="document.getElementById('addForm').submit();"><span></span></a>
表单已提交,但客户端验证不再起作用。
I'm changing an existing ASP.NET MVC2 application which uses the MicrosoftMvcValidation.js etc. for client side validation.
<h2>Details</h2>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Details", "Add", FormMethod.Post, new { id="addForm" }))
{ %>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Title) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Title) %>
<%: Html.ValidationMessageFor(model => model.Title) %>
</div>
...
<input type="submit" value="Create" />
The client side validation is working fine and dandy.
However, the designer wants to change the submit button to be a pretty anchor tag. I'm trying to submit the form using the anchor's onclick event i.e:
<a class="add-campaign-button-done" onclick="document.getElementById('addForm').submit();"><span></span></a>
The form submits, but client-side validation no longer works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需一点 jQuery,您就可以做到这一点,请参阅这篇文章: 手动调用 MVC 3 客户端验证以获取 ajax 帖子 了解更多信息,如果您需要更多帮助,请告诉我!
With a little bit of jQuery you can do the trick, see this post : Call MVC 3 Client Side Validation Manually for ajax posts for more informations and let me know if you need more help!