如何在jQuery表单验证上显示唯一的成功消息 [英] How to display unique success messages on jquery form validation

查看:85
本文介绍了如何在jQuery表单验证上显示唯一的成功消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望您能在此方面为我提供帮助,我目前正在使用该软件:

hope you can help me on this one, I'm currently using this:

jQuery插件:验证(主页)

我在这里一直在阅读相关的问题,但这是最接近的问题. httx://stackoverflow.com/questions/1863448/jquery-validation-on-success

I've been reading related questions here, but this one is the closest get. httx://stackoverflow.com/questions/1863448/jquery-validation-on-success

来自插件的文档 httx://docs.jquery.com/Plugins/Validation/validate#toption

from the plugin's documentation httx://docs.jquery.com/Plugins/Validation/validate#toption

成功 字符串,回调
如果指定,将显示错误标签以显示有效元素.如果给出了字符串,则将其作为类添加到标签.如果给出了Function,则将其标签(作为jQuery对象)作为唯一参数进行调用.可以用来添加"ok!"之类的文本.

success String, Callback
If specified, the error label is displayed to show a valid element. If a String is given, its added as a class to the label. If a Function is given, its called with the label (as a jQuery object) as its only argument. That can be used to add a text like "ok!".

目前,我唯一给我的对象是标签,并且我只能向其添加文本.现在,我想要的是拥有独特的成功信息.

Currently I'm only object given to me is the label, and I can only add text to it. Now what I want is to have unique success message.

例如: username字段将显示成功消息:'username okay!'
email ='电子邮件似乎正确'

For example: username field will have a success message: 'username okay!'
email = 'email seems right'

遵循这些原则,而不是在所有字段上仅显示一个通用的成功消息.

something along those lines, instead of displaying just one generic success message on all the fields.

到目前为止,我只尝试过这种方法:

I only tried this so far:

success: function(label) {
    label.text("Ok!").removeClass("error").addClass("success");
    },

编辑 只是想编辑,我想要的是一种访问输入元素并可能访问其属性的方法,以使用它显示在label.text上.例如label.text(element.attr("title");

EDIT just want to edit, what I wanted is a way to access the input element and probably access its attribute, to use it to display on the label.text. Something like label.text(element.attr("title"); for example.

推荐答案

我能想到的最简单的就是这样:

The simplest i can think of is as such:

jQuery(function($) {
    $('form').validate({
        success: function(label) {
            var name = label.attr('for');
            label.text(name+ ' is ok!');
        }
    });
});

<form action="" method="post">
    <input name="username" class="required" type="text" />
    <input type="submit" />
</form>

如果您想要更多独特的消息,也许可以将消息存储在某个地方(其他隐藏字段或其他DOM,或使用元数据).确保其位置一致(以便您可以在所有字段中遍历相同的位置)并相应地更新标签消息?

If you want more unique messages, maybe you can store your message somewhere (an additional hidden field, or additional DOM, or use metadata). Make sure it's in a consistent position (so you can traversing it is the same for all fields) and update the label message accordingly?

这篇关于如何在jQuery表单验证上显示唯一的成功消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆