javascript jQuery 验证 - 元素未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9346406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:21:13  来源:igfitidea点击:

jQuery validation - element is undefined

javascriptjqueryjquery-validate

提问by niao

I am using a jquery.validation plugin to validate my inputs on the form

我正在使用 jquery.validation 插件来验证我在表单上的输入

I use it as follows:

我使用它如下:

$(document).ready(function () {
jQuery(function () {
    // You can specify some validation options here but not rules and messages

    jQuery('form[id="reservationForm"]').validate({ ignore: ":not(:visible)" });

    $("#adult1Birthdate").mask("9999-99-99");
    $("#reservationPersonBirthdate").mask("9999-99-99");

    $("#adult1Firstname").rules("add", { required: true, minlength: 2, messages: { required: '<span style="color:Red">Pole wymagane</span>'} });
});

});

});

I get the following error however:

但是,我收到以下错误:

element is undefined

元素未定义

in the following line:

在以下行中:

var settings = $.data(element.form, 'validator').settings;

What am I doing wrong?

我究竟做错了什么?

**EDIT**

Also the following code does not work when I use jquery.validation

当我使用 jquery.validation 时,以下代码也不起作用

    $(document).ready(function () {
$(function () {

                $('a[id^="regionDetails"]').click(function () {
                    var vid_id = $(this).attr("id").replace("regionDetails", "#regionDetailsArea");
                    $('div[id^="regionDetailsArea"]').hide();
                    $(vid_id).show();
                    return false;
                });
            });
});

采纳答案by Blender

If you're using $, don't use jQuery. Try this:

如果您正在使用$,请不要使用jQuery。试试这个:

$(document).ready(function () {
  $('#reservationForm:visible').validate();

  $("#adult1Birthdate").mask("9999-99-99");
  $("#reservationPersonBirthdate").mask("9999-99-99");

  $("#adult1Firstname").rules("add", { required: true, minlength: 2, messages: { required: '<span style="color:Red">Pole wymagane</span>'} });

  $('a:not(#regionDetails)').click(function() {
    $('div:not(#regionDetailsArea)').hide();
    $($(this).attr("id").replace("regionDetails", "#regionDetailsArea")).show();

    return false;
  });
});

回答by Carlos

Make sure the element you're calling .rules()on does exist, jquery.validatedoesn't check for it when adding rules and fails miserably at the described line, simply because the element is not defined.

确保您正在调用的元素.rules()确实存在,jquery.validate在添加规则时不检查它,并且在描述的行中失败,仅仅是因为未定义该元素。

回答by gabemartinez

I've noticed that when this error appears for me, I have an input being called in rules that I don't have defined in my form. Or, vice versa.

我注意到,当我出现这个错误时,我在我的表单中没有定义的规则中调用了一个输入。或相反亦然。