MVC3不引人注目的验证:如何删除/重附上一组元素的验证? [英] MVC3 unobtrusive validation: how to remove/re-attach validation from a group of elements?

查看:112
本文介绍了MVC3不引人注目的验证:如何删除/重附上一组元素的验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是用例:

我有此长形式与字段的组,只有当用户进行某种选择中的可见输入之一变为可见。阅读我以为jQuery.validator.unobtrusive.parse主体('额外数据),其中.extra数据是一类隐藏分区的布拉德·威尔逊的文章。没有运气的数据已经在那里当第一个解析已完成。

I have this long form with group of field that only becomes visible if the user makes a certain selection in one of the visible inputs. Reading Brad Wilson's post on the subject I thought jQuery.validator.unobtrusive.parse('.extra-data') where .extra-data is a class of a hidden div. No luck as the data was already there when the first parse was done.

因此​​,在我想出了这个月底删除该规则:

So at the end I came up with this to remove the rules:

$('.data-panel').find('input[type="text"], textarea, select').each(function (i, item) {
    var currentRules = $(item).rules('remove'); // Saving removed rules to a sorta dictionary
    if (!$.isEmptyObject(currentRules)) {
        removedRules[$(item).attr("name")] = currentRules;
    }
 });

和这重新连接它们:

$('.data-panel').find('input[type="text"], textarea, select').each(function (i, item) {
    if (!$.isEmptyObject(removedRules[$(item).attr('name')])) {
        $(item).rules('add', removedRules[$(item).attr('name')]);
    }
});

问题是,那感觉有点哈克因为我硬是在各字段删除并重新安装的验证规则去。我的问题是:是不是有一个更简单的方法?性能也是一个问题,在那些巨大的一种形式,你可以感受到点击验证运行之间的延迟。

Problem is, it feels kinda hacky as I'm literally going through each field removing and re-attaching those validation rules. My question is: isn't there a more straightforward way? Performance is also an issue, in one of those huge forms you can feel the delay between the click and the validation run.

推荐答案

不要拆卸和重新连接的规则。刚禁用启用输入。残疾人领域不参加验证,他们也不得到提交到服务器。

Do not remove and re-attach rules. Just disable or enable inputs. Disabled fields do not participate in validation, neither do they get submitted to server.

//disable inputs. No validation will occur on these
$('.data-panel').find('input[type="text"], textarea, select').attr('disabled', 'disabled');

//enable inputs. Validation is re-enabled
$('.data-panel').find('input[type="text"], textarea, select').removeAttr('disabled');

这篇关于MVC3不引人注目的验证:如何删除/重附上一组元素的验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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