【问题标题】:Using jQuery Validate's invalidHandler event使用 jQuery Validate 的 invalidHandler 事件
【发布时间】:2011-06-04 18:07:18
【问题描述】:

我需要在 div 中添加一个类,该类在验证时包裹在 select 元素周围。

$("#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");
        });
    }
});

【问题讨论】:

    标签: javascript jquery validation jquery-plugins jquery-validate


    【解决方案1】:

    你可以使用.valid()方法,像这样:

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

    但是,highlight and unhightlight options 专门用于此:

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

    【讨论】:

      【解决方案2】:

      看起来这段代码是正确的:

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

      也许你的问题在于它周围的代码......尝试将你的 addClass 行移到它所在的函数之外,看看它是否在那里工作。

      【讨论】:

        【解决方案3】:

        invalidHandler 仅在表单提交且无效时调用。我相信仅在该函数下添加您的代码就足够了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-26
          • 2013-03-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多