ParlseyJS-从禁用字段中删除验证 [英] ParlseyJS - remove validation from disabled fields

查看:94
本文介绍了ParlseyJS-从禁用字段中删除验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始将ParlseyJS用于我们正在做的一些新工作.

just started using ParlseyJS for some new work we are doing.

我遇到一个问题,即我根据下拉值(以表格形式)隐藏和禁用一堆下拉列表.当我提交此表单时,这些字段将继续得到验证.这些字段已通过jQuery禁用,例如:

I am having an issue whereby I hide and disable a bunch of dropdown lists based on a dropdown value (in a form). When I submit this form, these fields continue to be validated. These fields are being disabled via jQuery as such:

minage.removeAttr('data-parsley-minagecheck').attr('data-parsley-excluded', '').attr('disabled', 'disabled');
maxage.removeAttr('data-parsley-maxagecheck').attr('data-parsley-excluded', '').attr('disabled', 'disabled');
xxx.removeAttr('data-parsley-xxxcheck').attr('data-parsley-excluded', '').attr('disabled', 'disabled');
yyy.removeAttr('data-parsley-yyycheck').attr('data-parsley-excluded', '').attr('disabled', 'disabled');

您会注意到,我还将删除自定义验证检查,并将排除的字段添加到下拉列表的属性中.

You will notice that I am also removing the custom validation checks and I am adding the excluded field to the attributes of the dropdown lists.

如何阻止他们通过验证?

How can I stop them from being validated?

我更新了Parsley.js文件,将ParsleyDefaults设置为:

I updated my Parsley.js file to set ParsleyDefaults as such:

excluded: 'input[type=button], input[type=submit], input[type=reset], input[type=hidden], [disabled]',

推荐答案

将Parsley绑定到表单后,仅删除属性是不够的.这是因为Parsley将创建具有该表单约束的ParsleyForm对象.

After you bind Parsley to your form, it is not enough to remove the attributes. This is because Parsley will create a ParsleyForm object with the constraints for that form.

此外,在将Parsley绑定到表单时,将考虑excluded选项.对于您而言,这些字段尚未禁用,因此出于验证目的将考虑这些字段.

Also, the excluded option will be taken into account at the moment where Parsley is binded to the form. In your case, the fields are not yet disabled, so they will be taken into account for validation purposes.

您需要的是在删除属性后销毁并应用欧芹,因此ParsleyForm不包含这些字段.如果您使用的是Parsley v2,则应在删除或插入属性后添加以下代码:

What you need is to destroy and apply parsley after you have removed the attributes, so ParsleyForm doesn't containt those fields. If you are using Parsley v2 you should add this code after the removal or insertion of the attributes:

$("#myForm").parsley().destroy();
$("#myForm").parsley();

还要注意,从jQuery 1.6开始, .attr()状态

Also take note, as of jQuery 1.6 the .attr() states

从jQuery 1.6开始,.attr()方法对尚未设置的属性返回undefined. 要检索和更改DOM属性,例如表单元素的checkedselecteddisabled状态,请使用.prop()方法.

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.

所以您应该使用

minage.removeAttr('data-parsley-minagecheck')
    .attr('data-parsley-excluded', '').prop('disabled', true);

这篇关于ParlseyJS-从禁用字段中删除验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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