jQuery validate插件无法忽略复选框

时间:2013-05-23 15:57:29

标签: jquery jquery-validate

我有一个AJAX表单,我有很多文本字段。只要我通过MVC框架添加一个复选框(生成的HTML代码如下),这些文本字段就可以正常验证,没有问题。

<input id="NotifyLocation" class="check-box" type="checkbox" value="true"
 name="NotifyLocation" data-val-required="The NotifyLocation field is required." 
 data-val="true" checked="checked"></input>

jQuery插件会在plguin中的此代码中抛出错误。

attributeRules: function(element) {
    var rules = {};
    var $element = $(element);

    for (var method in $.validator.methods) {
        var value;
        // If .prop exists (jQuery >= 1.6), use it to get true/false for required
        if (method === 'required' && typeof $.fn.prop === 'function') {
            value = $element.prop(method);
        } else {
            value = $element.attr(method);
        }
        if (value) {
            rules[method] = value;
       //ERROR is thrown on the next line
        } else if ($element[0].getAttribute("type") === method) {
            rules[method] = true;
        }
    }

    // maxlength may be returned as -1, 2147483647 (IE) and 524288 (safari) for text inputs
    if (rules.maxlength && /-1|2147483647|524288/.test(rules.maxlength)) {
        delete rules.maxlength;
    }

    return rules;
}

错误消息是“ TypeError:$ element [0]未定义”。我尝试添加以下代码以强制jQuery忽略我的ready方法中的复选框

 $("#frmAjaxLocationUpdater").validate({
        ignore: "#NotifyLocation *"
    });

但我仍然在jQuery验证插件中遇到同样的错误。我试图按类添加规则仍然是同一个问题。根据许多帖子,这应该有效,但事实并非如此。

任何想法是怎么回事?是什么导致这种情况?

谢谢。

1 个答案:

答案 0 :(得分:1)

这是一段解决了我的问题的JavaScript代码

document.getElementById("NotifyLocation").setAttribute("data-val","false") ;
相关问题