0

我有一个 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] is undefined ”。我尝试添加以下代码以强制 jQuery 忽略我的就绪方法中的复选框

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

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

有什么想法吗?什么会导致这种情况?

谢谢你。

4

1 回答 1

1

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

document.getElementById("NotifyLocation").setAttribute("data-val","false") ;
于 2013-05-24T16:40:28.510 回答