MVC表单验证在多个选项卡 - 自动跳转到标签与验证错误? [英] MVC form validation in multiple tabs - auto jump to tab with validation errors?

查看:685
本文介绍了MVC表单验证在多个选项卡 - 自动跳转到标签与验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个选项卡标签栏。在每个选项卡,我有许多供用户输入文本字段。
标签栏是一种形式,只是下方的提交按钮包围。

I have tabstrip with multiple tabs. In each tab I have a number of text fields for the user to input. The tabstrip is surrounded by a form and just below a submit button.

我对模型属性的注解验证。验证使用jQuery验证工作正常。然而,如果用户作出输入错误中的一个领域,进入到不同的标签,preSS提交将出现在非活性标签中的误差,因而不能被用户看到。我想自动喜欢jQuery验证去验证出错的标签,这样用户就可以看到它。这可能吗?

I have annotated validation on the model attributes. Validations works fine using Jquery validation. However, if the user makes an input error in a field, goes to a different tab and press submit the error will appear in the inactive tab and thus not be seen by the user. I would like the Jquery validation automatically to go to the tab with the validation error, so the user can see it. Is this possible?

推荐答案

我不认为有出这个盒子的解决方案。但是你可以在JavaScript pretty容易做到这一点。你要做的就是在表格提交你看每个选项卡的内容,如果你发现验证错误,那么你切换到该选项卡。

I don't think that there is out of the box solution for this. But you can do it in javascript pretty easy. What you do is on form submit you look at content of each tab, and if you find validation error then you switch to that tab.

下面是示例:

<script type="text/javascript">
    $(document).ready(function () {
        $("#myForm").submit(function () {
            $("#tabs").tabs("select", $("#myForm .input-validation-error").closest(".ui-tabs-panel").get(0).id);
        });
    });
</script>

本示例假定您的窗体的ID是myForm会,你的标签ID是标签。此外,它假定您已经ClientValidationEnabled =真实UnobtrusiveJavaScriptEnabled =在web.config中正确的。这code片段将切换到第一个标签错误。

This sample assumes that your form's id is myForm, that your tab id is tabs. Also it assumes that you have ClientValidationEnabled = true and UnobtrusiveJavaScriptEnabled = true in web.config. This code snippet will switch to first tab with error.

这code只是样品,并可能进行重构,但它显示的想法。

This code is just sample and could be refactored, but it shows idea.

这篇关于MVC表单验证在多个选项卡 - 自动跳转到标签与验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆