错误消息出现在错误的地方,使用jQuery验证插件 [英] error message appears in wrong place using jquery Validation plugin

查看:90
本文介绍了错误消息出现在错误的地方,使用jQuery验证插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一页中使用Laravel,bootstrap和jquery,可悲的是,我对所有这些人都是新手.我遇到的问题是使用jquery Validation插件将错误消息显示在错误的位置,在下图中,错误位置的消息被突出显示.如您所见,每个无线电都包含在标签标签中,这在Bootstrap CSS中是必需的.我试图找出该消息之后插入的元素,但失败了.有人可以告诉我应该更改代码的哪一部分,以在所有收音机发出信号后移动错误消息吗?

I'm trying to use Laravel, bootstrap and jquery in one page, sadly I'm a newbie to all of them. The problem I've met is the error message displays in wrong place using jquery Validation plugin, in the picture below, the message in wrong place is highlighted. As you can see, each radio is included in a label tag, it's required in Bootstrap CSS. I tried to find out which element the message was inserted after, and I failed. Can anybody tell me which part of the code should be changed to move the error message after all radios and how?

推荐答案

我似乎总是遇到同样的问题.您可以使用errorPlacement参数解决该问题.

I always seem to have the same problem. You can solve it with the errorPlacement parameter.

$('form').validate({

    errorPlacement: function(error, element) {

        // Get inputs with this name
        var obj = $('[name="'+element.attr('name')+'"]');

        // Are there multiple?
        if (obj.length > 1) {
            // Add error after whatever the parent element is of the last one
            error.insertAfter(obj.last().parent());
        } else {
            // Default, add error after the input
            error.insertAfter(obj);
        }
    }
});

您必须弄清楚如何处理它,这只是一种方法.

You'll have to figure out how you want to handle it, this is just one way.

演示: http://jsfiddle.net/PgAyp/

这篇关于错误消息出现在错误的地方,使用jQuery验证插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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