简体   繁体   English

jQuery表单验证,显示成功消息

[英]jQuery form validation, show success message

The following jQuery is working very well, now I am trying to show a success message, but I can't get it to work. 以下jQuery运行良好,现在我试图显示成功消息,但无法正常工作。 This is my attempt. 这是我的尝试。

            jQuery("#name").validate({
                expression: "if (VAL.length > 5 && VAL)  return true; else return false;",
                message: "Please enter more than 5 characters",
                success: function() {
                $(this).toggleClass("ok");
                }
            });

I would like to show a div with the class 'ok'. 我想显示一个类为“ ok”的div

This is the div 这是div

<div class="ok"> OK! </div>

and this is the css 这是CSS

.ok { display: none; color: green; }
.ok { color: green; }

Remove display:none from your css, (move it to the DIV's style attribute so you can toggle it with jQuery) 删除display:none从您的CSS,(将其移动到DIV的样式属性,以便您可以使用jQuery切换它)

<div id="successMessage" style="display:none;" > OK! </div>

Then make your success method: 然后制定您的成功方法:

success: function() {
     $('#successMessage').show();
}

I don't know the validate() method well, so I'm not sure what $(this) refers to inside of the success method, so I used an ID selector. 我不太了解validate()方法,所以我不确定$(this)在成功方法中指的是什么,所以我使用了ID选择器。 See the documentation for show . 请参阅show的文档。

if i understand correctly, you are very close! 如果我理解正确,那么您非常亲密!

just give your div an id: 只需给您的div一个ID:

<div id="successMessage" class="ok"> OK! </div>

then in your validate instead of using this, select your div by id: 然后在您的验证而不是使用此方法中,按ID选择div:

$("#successMessage").show();

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

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