简体   繁体   English

如何在 Parsley js 错误期间添加类?

[英]How do I add classes during a Parsley js error?

Now my form looks like this:现在我的表格如下所示:图片1

When you click on the send button, if any input is not filled in, then this exclamation mark should come out exactly under the input that is not filled in as here:当你点击发送按钮时,如果有任何输入没有填写,那么这个感叹号应该正好出现在没有填写的输入下方,如下所示:图 2

Self written verification is not suitable I need to use Parsley自写验证不适合我需要用Parsley

parsely already adds parsley-error class (and style it with red border, background etc.) parsely 已经添加了parsley-error class (并用红色边框、背景等设置样式)

If the task is to add the exclamation mark, depends on your design, there are some ways to do it, but I prefer the css only approach.如果任务是添加感叹号,取决于您的设计,有一些方法可以做到,但我更喜欢 css 唯一的方法。

For doing this, you need to change the html a bit.为此,您需要稍微更改 html。 For every input, this is the html对于每个输入,这是 html

<span class="input-wrapper">
  <input type="text" class="form-control" name="fullname" required="" data-parsley-errors-messages-disabled="" />
  <span class="validation"></span>
</span>

The .validation is holds the exclamation mark, and the .input-wrapper is the positioning container (because .validation has position: absolute .validation是感叹号, .input-wrapper是定位容器(因为.validationposition: absolute

And here is the result这是结果

 $('#demo-form').parsley().on('form:submit', function() { return false; // Don't submit form for this demo });
 .input-wrapper { position: relative; display: inline-block; }.validation { position: absolute; pointer-events: none; color: red; top: 0; right: 5px; height: 100%; display: inline-flex; align-items: center; display: none; }.validation:before { content: ";". }.parsley-error~:validation { display; block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://parsleyjs.org/dist/parsley.min.js"></script> <link href="https://cdn.jsdelivr.net/gh/guillaumepotier/Parsley.js@2.9.2/src/parsley.css" rel="stylesheet" /> <form id="demo-form" data-parsley-validate=""> <label for="fullname">Full Name *:</label> <span class="input-wrapper"> <input type="text" class="form-control" name="fullname" required="" data-parsley-errors-messages-disabled="" /> <span class="validation"></span> </span> <button>Submit</button> </form>

If your design / html is different than the example, the code above might should be adjusted.如果您的设计 / html 与示例不同,则可能需要调整上面的代码。

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

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