jquery 验证 - 尝试验证单选按钮和复选框按钮并使用 ErrorPlacemant 在每个组下方显示错误

发布于 2024-12-06 15:04:14 字数 345 浏览 1 评论 0原文

新手来了 我正在尝试让单选按钮和复选框按钮进行验证。

错误消息应出现在最后一个单选/复选框组下

单选按钮需要选择 检查按钮需要至少选择一个

--- 在媒体上,因为所有按钮都有一个唯一的 ID,所以我将其视为一个组,但迷失了 addMethod 来验证组。

--- 另外在 errorPlacement 上,如果我没有元素 ID,我该怎么办?

http://jsbin.com/aqekah/14

基本上没问题,但现在我得到了错误的 InValids 数量我的警报

Newbie here
I am trying to get Radio buttons and checkbox buttons to validate.

Error message should appear under the last it's groups of radio/checkbox

The radio buttons requires a selection
The check buttons Required one selected minimum

--- On the Media, since all have a unique ID I treated it as a group, but am lost on
addMethod to validate group.

--- Also on errorPlacement what do I do if I don't have an Element ID?

http://jsbin.com/aqekah/14

Mostly OK, but now I get wrong number of InValids in my alert

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2024-12-13 15:04:14

事实上,我们前几天就遇到了这个问题,这就是我解决它的方法:

var validator = $('form#yourID').validate({
        errorPlacement: function (error, element) {
            if (element.is(":radio")) {
                element.closest('.rbl').after(error);
                element.closest('.rbl').addClass('error');
            }
            else if (element.is(":checkbox")) {
                element.closest('.cbl').after(error);
                element.closest('.cbl').addClass('error');
            }
            else { // This is the default behavior of the script
                error.insertAfter(element);
            }
        },
        rules: { ...

其中 .rbl 是一个 标记,看起来像这样,带有以下 css :

<table cellpadding="0" cellspacing="0" class="rbl">
<tr><td><label><input type="radio" name="myradio" value="1"> Radio 1</label></td></tr>
<tr><td><label><input type="radio" name="myradio" value="2"> Radio 2</label></td></tr>
</table>

css:

FORM .rbl
{
    float: left;
}
FORM .rbl TD
{
    padding: 4px 24px 0 0;
}

同样的情况也适用于带有 .cbl 的复选框列表。所有其他非单选和复选框类型将使用标准错误放置。

希望有帮助。

We had this come up just the other day actually, this is how I resolved it:

var validator = $('form#yourID').validate({
        errorPlacement: function (error, element) {
            if (element.is(":radio")) {
                element.closest('.rbl').after(error);
                element.closest('.rbl').addClass('error');
            }
            else if (element.is(":checkbox")) {
                element.closest('.cbl').after(error);
                element.closest('.cbl').addClass('error');
            }
            else { // This is the default behavior of the script
                error.insertAfter(element);
            }
        },
        rules: { ...

Where .rbl is a <table> tag and looks like this with the following css:

<table cellpadding="0" cellspacing="0" class="rbl">
<tr><td><label><input type="radio" name="myradio" value="1"> Radio 1</label></td></tr>
<tr><td><label><input type="radio" name="myradio" value="2"> Radio 2</label></td></tr>
</table>

css:

FORM .rbl
{
    float: left;
}
FORM .rbl TD
{
    padding: 4px 24px 0 0;
}

The same can be applied for checkbox lists with a .cbl. All other non-radio and checkbox types will use standard error placement.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文