jQuery Validation插件自定义错误位置 [英] Jquery Validation plug-in custom error placement

查看:109
本文介绍了jQuery Validation插件自定义错误位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用jQuery Validation插件的以下形式:

Using the jQuery Validation plug-in for the following form:

<form id="information" method="post" action="#">

            <fieldset>
                <legend>Please enter your contact details</legend>
                <span id="invalid-name"></span>
                <div id="id">
                    <label for="name">Name: (*)</label>
                    <input type="text" id="name" class="details" name="name" maxlength="50" />
                </div>

                <span id="invalid-email"></span>
                <div id="id">
                    <label for="email">Email: (*)</label>
                    <input type="text" id="email" class="details" name="email" maxlength="50" />
                </div>
            </fieldset>
            <fieldset>
                <legend>Write your question here (*)</legend>
                <span id="invalid-text"></span>
                <textarea  id="text" name="text" rows="8" cols="8"></textarea>


            <div id="submission">
                <input type="submit" id="submit" value="Send" name="send"/>
            </div>
            <p class="required">(*) Required</p>
            </fieldset>

             </form>

如何将错误放置在span标记内? (#invalid-name,#invalid-email,#invalid-text)

How can I place the errors inside the span tags? (#invalid-name, #invalid-email, #invalid-text)

我阅读了有关错误放置的文档,但是我没有得到它的工作方式. 是否可以处理每个错误并将其放置在指定的元素中?

I read the documentation about error placement but I did not get how it works. Is it possible to handle each single error and place it in the specified element?

谢谢

推荐答案

这是一个基本结构,您可以在方法中使用所需的任何选择器.您有错误元素和无效元素.

This is a basic structure, you can use whatever selector you would like in the method. You have the error element and the element that was invalid.

jQuery.validator.setDefaults({
    errorPlacement: function(error, element) {
        error.appendTo(element.prev());
    }
});

或者可以定位ID,您可以

Or to target the ID, you could do

jQuery.validator.setDefaults({
    errorPlacement: function(error, element) {
        error.appendTo('#invalid-' + element.attr('id'));
    }
});

未经测试,但应该可以工作.

Not tested, but should work.

这篇关于jQuery Validation插件自定义错误位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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