如何使用parsley.js订阅单个字段?

ste*_*han 2 jquery parsley.js

我试图找出一种方法来改变成功验证字段的css元素(使用parsley.js).以下是我给出的回复,我不知道如何做到这一点.

"解决方案是在全球范围内或通过订阅单个表格或字段来倾听正确的事件.欧芹事件:http://parsleyjs.org/doc/index.html#psly-events-overview

你可能想要parsley:field:success和parsley:field:error events.

一旦你有了一个事件处理程序,就可以使用jQuery提供各种策略 - 你可以检查元素中的成功类"

我真的不明白如何完成上述响应中描述的内容.

在成功验证表单字段后,如何将div的显示更改为"none"?

Luí*_*ruz 5

Parsley有一个事件列表,当事情发生时触发.您可以查看文档中的完整列表.

假设您想要在验证某些字段时隐藏div(反之,您将希望在未验证字段时显示div),您可以使用事件parsley:field:success并按parsley:field:error响应中的建议使用.

要使用这些事件,您需要听取它们,以便执行某些操作.您可以通过$.listen('event:name', callback)(在文档中使用)来实现.

所以你需要的是这样的:

// Listen to the event triggered when SOME field is validated 
$.listen('parsley:field:success', function(parsleyField) {
    // We need to check what is the validated field
    if (parsleyField.$element.attr('name') == 'some-form-field') {
        // given that this is the field we want, we'll hide the div
        $('#div-to-hide').hide();
    }
});

// This is needed in order to display the div after the field was validated.
// Imagine that the field was validated and then the user changed its content
// and now the field is not valid
$.listen('parsley:field:error', function(parsleyField) {
    if (parsleyField.$element.attr('name') == 'some-form-field') {
        $('#div-to-hide').show();
    }
});
Run Code Online (Sandbox Code Playgroud)