最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using jQuery Validate's invalidHandler event - Stack Overflow

programmeradmin1浏览0评论

I need to add a class to div that is wrapped around the select element upon validation.

$("#Form").validate({
    invalidHandler: function (form, validator) {
        $("[id$='_DropDownList']").each(function () {
            // How do I figure out if $(this) is valid or invalid?
            // Add the class below if invalid.
            $(this).parent().addClass("error");
        });
    }
});

I need to add a class to div that is wrapped around the select element upon validation.

$("#Form").validate({
    invalidHandler: function (form, validator) {
        $("[id$='_DropDownList']").each(function () {
            // How do I figure out if $(this) is valid or invalid?
            // Add the class below if invalid.
            $(this).parent().addClass("error");
        });
    }
});
Share Improve this question edited Jan 6, 2011 at 16:36 Nick Craver 631k138 gold badges1.3k silver badges1.2k bronze badges asked Jan 6, 2011 at 16:08 mhenrymhenry 1,4875 gold badges18 silver badges31 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

You can use the .valid() method, like this:

$("#Form").validate({
    invalidHandler: function (form, validator) {
        $("[id$='_DropDownList']").each(function () {
            if(!$(this).valid())
              $(this).parent().addClass("error");
        });
    }
});

However, the highlight and unhightlight options are specifically for this:

$("#Form").validate({
  highlight: function(element, errorClass, validClass) {
    $(element).parent().removeClass(validClass).addClass(errorClass);
  },
  unhighlight: function(element, errorClass, validClass) {
    $(element).parent().removeClass(errorClass).addClass(validClass);
  }
});

It looks like this code is correct:

$(this).parent().addClass("error");

Maybe your issue is with the code surrounding it... Try moving your addClass line outside the function it is in to see if it work there.

invalidHandler is only called when the form is submitted and not valid. I believe adding your code under that function alone is enough.

发布评论

评论列表(0)

  1. 暂无评论