如何使用 parsley.js 订阅单个字段?
How to subscribe to an individual field using parsley.js?
我正在尝试找出一种方法来更改 css 字段成功验证后的元素(使用 parsley.js)。以下是我收到的回复,我不明白如何完成此操作。
"A solution is to listen for the right events, either
globally or by subscribing on the individual forms or fields. Parsley
events: http://parsleyjs.org/doc/index.html#psly-events-overview
You'll probably want the parsley:field:success and parsley:field:error
events.
Once you've got an event handler, there are all sorts of strategies
available to you using jQuery - you can check for success classes in
the elements"
我不太明白如何完成上面回复中描述的内容。
如何在成功验证表单字段后将 div 的显示更改为 'none'?
Parsley 有一个事件列表,当某些事情发生时会触发这些事件。您可以查看完整的list in the docs。
鉴于您希望在验证某些字段时隐藏 div(相反,您希望在未验证字段时显示 div),您可以使用事件 parsley:field:success
和 parsley:field:error
如回复中所建议。
为了使用这些事件,您需要监听它们以便执行一些操作。您可以通过 $.listen('event:name', callback)
(usage in docs).
所以你需要的是这样的东西:
// 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();
}
});
我正在尝试找出一种方法来更改 css 字段成功验证后的元素(使用 parsley.js)。以下是我收到的回复,我不明白如何完成此操作。
"A solution is to listen for the right events, either globally or by subscribing on the individual forms or fields. Parsley events: http://parsleyjs.org/doc/index.html#psly-events-overview
You'll probably want the parsley:field:success and parsley:field:error events.
Once you've got an event handler, there are all sorts of strategies available to you using jQuery - you can check for success classes in the elements"
我不太明白如何完成上面回复中描述的内容。
如何在成功验证表单字段后将 div 的显示更改为 'none'?
Parsley 有一个事件列表,当某些事情发生时会触发这些事件。您可以查看完整的list in the docs。
鉴于您希望在验证某些字段时隐藏 div(相反,您希望在未验证字段时显示 div),您可以使用事件 parsley:field:success
和 parsley:field:error
如回复中所建议。
为了使用这些事件,您需要监听它们以便执行一些操作。您可以通过 $.listen('event:name', callback)
(usage in docs).
所以你需要的是这样的东西:
// 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();
}
});