jQuery 验证 - 突出显示单选标签

发布于 2024-08-24 23:03:35 字数 606 浏览 8 评论 0原文

我尝试使用 jQuery 验证仅突出显示单选按钮的标签,而不突出显示其他输入的标签。我的单选按钮集有一个名为“类型”的标签。我似乎无法让它发挥作用!

$(document).ready(function(){
    $("#healthForm").validate({

 highlight: function(element, errorClass) {
     $(element).addClass(errorClass)
     $(element.form).find("label[for='type']")
                    .addClass("radioerror");
  },
  unhighlight: function(element, errorClass) {
     $(element).removeClass(errorClass)
     $(element.form).find("label[for='type']")
                    .removeClass("radioerror");
  },
    errorPlacement: function(error, element) {
        }
    });
  });

I'm trying to use jQuery validation to highlight the labels for my radio buttons only, and not the labels for my other inputs. I have a label for my radio button set called 'type'. I can't seem to get it to work!

$(document).ready(function(){
    $("#healthForm").validate({

 highlight: function(element, errorClass) {
     $(element).addClass(errorClass)
     $(element.form).find("label[for='type']")
                    .addClass("radioerror");
  },
  unhighlight: function(element, errorClass) {
     $(element).removeClass(errorClass)
     $(element.form).find("label[for='type']")
                    .removeClass("radioerror");
  },
    errorPlacement: function(error, element) {
        }
    });
  });

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

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

发布评论

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

评论(2

青巷忧颜 2024-08-31 23:03:35

我不太熟悉 jQuery 的验证插件,但这似乎不对:

$(element.form).find("label[for='type']")

首先,我认为 $(element.form) 应该是 $(element).closest('形式')?我可能是错的:P

其次, .find("label[for='type']") 与任何其他元素一样与“element”无关以相同的形式将获取相同的标签。

如果标签是单选按钮的同级标签,您可能需要直接使用 sibling()

I'm not really familiar with jQuery's validation plugin, but this one doesn't seem right:

$(element.form).find("label[for='type']")

Firstly, I think $(element.form) should be $(element).closest('form')? I could be wrong :P

Secondly, .find("label[for='type']") has got nothing to do with the "element" as any other elements in the same form will fetch the same label.

If the label is the sibling of the radio button, you might want to use sibling() directly.

我的鱼塘能养鲲 2024-08-31 23:03:35

我将您的代码更改为以下内容,然后它就可以工作了!

$(document).ready(function() {
    $("#healthForm").validate({
        highlight: function(element, errorClass) {

            if(element.type == 'radio') {
                $(element.form).find('[name="' + element.name + '"]').each(function(){
                    var $this = $(this);
                    $this.closest('label[for="' + $this.attr('id') + '"]').addClass(errorClass);
                });
            }

        },
        unhighlight: function(element, errorClass) {

            if(element.type == 'radio') {
                $(element.form).find('[name="' + element.name + '"]').each(function(){
                    var $this = $(this);
                    $this.closest('label[for="' + $this.attr('id') + '"]').removeClass(errorClass);
                });
            }

        },
        errorPlacement: function(error, element) {
        }
    });
});

I changed your code to the following and then it works!

$(document).ready(function() {
    $("#healthForm").validate({
        highlight: function(element, errorClass) {

            if(element.type == 'radio') {
                $(element.form).find('[name="' + element.name + '"]').each(function(){
                    var $this = $(this);
                    $this.closest('label[for="' + $this.attr('id') + '"]').addClass(errorClass);
                });
            }

        },
        unhighlight: function(element, errorClass) {

            if(element.type == 'radio') {
                $(element.form).find('[name="' + element.name + '"]').each(function(){
                    var $this = $(this);
                    $this.closest('label[for="' + $this.attr('id') + '"]').removeClass(errorClass);
                });
            }

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