简体   繁体   English

如何使用Parsley.js成功更改每个字段的CSS属性

[英]How to change CSS properties on success of each field with Parsley.js

I asked this question many months ago and it was never answered, but I would like to try again with more specific details. 我几个月前曾问过这个问题,但从未得到解答,但我想再尝试更具体的细节。

I'm using parsley.js and currently whenever a form field is validated, that field turns green, that's great. 我正在使用parsley.js,目前每当验证表单字段时,该字段就会变为绿色,这很好。 But what I'm trying to do is add a javascript function (or I don't know if there is a built in way to do it with parsley) that changes a css property on success. 但是我想做的是添加一个javascript函数(或者我不知道是否有使用香芹做的内置方法),该函数会在成功时更改css属性。 Specifically I just want to change a div's display from none to inline. 具体来说,我只想将div的显示从无显示更改为嵌入式显示。 At the end of the day I'm just trying to have a green checkmark appear next to each field when validated. 归根结底,我只是想在验证时在每个字段旁边显示一个绿色的选中标记。

Any help would be appreciated. 任何帮助,将不胜感激。

Also I don't know if this helps but here I copied this from Chrome Console. 另外,我不知道这是否有帮助,但是我从Chrome控制台复制了此内容。

<input data-parsley-maxlength="22" data-parsley-maxlength-message="You've exceeded the recommended space" data-parsley-trigger="change" data-required="true" id="id_headline" maxlength="200" name="headline" type="text" data-parsley-id="3782" class="parsley-success">

An extremely easy solution is a CSS class based one - have your checkmark and the input in a div, tell parsley to place the success class on the parent, and then display the checkmark with a class selector. 一个非常简单的解决方案是基于CSS类的解决方案-将您的选中标记和输入内容放在div中,告诉欧芹将成功类放在父级上,然后使用类选择器显示选中标记。 Here's an example: 这是一个例子:

HTML HTML

<form id="demo-form" data-parsley-validate>
    <div class="input-holder">
        <input name="id-number" type="number" data-parsley-trigger="keyup" data-parsley-class-handler=":parent">
        <div class="checkmark">CHECKMARK</div>
    </div>
</form>

CSS CSS

.checkmark {
    display: none;
}
.input-holder.parsley-success .checkmark {
    display: block;
}

http://jsfiddle.net/kuJPL/25/ http://jsfiddle.net/kuJPL/25/

Another possible 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 欧芹事件: http : //parsleyjs.org/doc/index.html#psly-events-overview

You'll probably want the parsley:field:success and parsley:field:error events. 您可能需要parsley:field:successparsley:field:error事件。

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 you care about and apply a CSS class to sibling elements, for example. 一旦有了事件处理程序,就可以使用jQuery使用各种策略-例如,您可以在自己关心的元素中检查成功类,并将CSS类应用于同级元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM